Silverstripe 4:
How to write a query to allowed in ss file and show on screen?
I want write like $getType = ProductType::get()->filter…
but i dont know how to write. I get Types when i write in sqlQuery format
[$sqlQuery = " SELECT Title FROM Devmac_Object_ProductType WHERE ID IN ($strProductListID)";
$objResult = DB::query($sqlQuery);
Not sure I understand, but you need to append first()
or last()
if you want a single instance otherwise you will always get a list, e.g.:
$type = ProductType::get()->filter(...)->first();
I want to write below query into your suggested query format,
sqlQuery = " SELECT Title FROM Devmac_Object_ProductType WHERE ID IN ($strProductListID)";
Here,
- Title is Column Name.
- Devmac_Object_ProductType is Table Name.
- $strProductListID is array of id (i.e. 2,3,4,4)
But I want the whole Title’s which matches with those ids.
Thank you for the suggestion and quick replay.
No idea why you prefix an array of IDs with $str
, but something like the following code should give you the idea:
$matches = ProductType::get()->filter([ 'ID' => [ 2, 3, 4 ] ]);
1 Like