Filtering by Child Objects

The property that you search on does not have to be a property of the parent entity - you can use child objects - even with one-to-many relationships. To specify a child, use the property name of the property that contains the child (or children), followed by a dot, followed by the property on the child object that you want to filter by. You can even go further than an immediate child, and filter by 'grandchild' properties, by just continuing with the dot notation.

For example, if you have a parent entity which has a property with a one-to-one relationship called child, and the child entity has a property with a one-to-many relationship called pets, you can search for all parents which have a child who has a dog, like this:

$query = QueryBuilder::create()
    ->where('child.pets.type', QB::EQUALS, 'dog')
    ->buildSelectQuery();

If you want to embed a property path in some SQL (eg. to use a MySQL function like CONCAT), you can do so by wrapping the property path in percent delimiters, eg:

$query = QueryBuilder::create()
    ->where("CONCAT(%child.pets.type%, %child.pets.name%)", '=' 'DogRover')
    ->buildSelectQuery();

Last updated