Search code examples
phpsqlcontent-management-systemrelationshippimcore

How to use object listing for condition on relation object


This question may be asked before, But i unable to find satisfiable answer. Let we have object named Product having a relation with Tags. So if we need to find products for particular Tag, As pimcore object listing work on direct on main table/view that store relations as comma seperated values in single column. So only LIKE search solution is suggested by others.

$entries ->setCondition("Tags LIKE " . $entries->quote("%".$tagId."%"));

But I think, this solution have one bigger issue If some products store tags id like 00,111,112,189 and we want to search on tag id = 11 It will return products for undesirable tag ids like 111, 112 record too.

One solution to ignore pimcore object at all and use Zend DB Calls to object_[PRODUCTCLASSID] AND object_relations_[PRODUCTCLASSID] views.

If any other solution exist to tackle issue using only Pimcore Object API.


Solution

  • As far as I know relations are saved like this: ,00,111,112,189,. So with comma in the beginning and the end.

    This enables you to use your condition like this:

    $entries ->setCondition("Tags LIKE " . $entries->quote("%,".$tagId.",%"));
    

    Notice the comma after and before the % sign.

    Source: Filtering for relations via PHP API