I want to get some objects with part of their downloadable associations.
This is a self-referencing hierarchical relationship, with a table like:
CREATE TABLE entities (
id serial PRIMARY KEY,
parent_id integer REFERENCES entities,
attribute_id integer REFERENCES attributes,
[...]
);
Now I want to preload the object with some of its children by matching a WHERE clause, such as entity.attribute_id = ?.
I have this in regular SQL, like this:
SELECT * FROM entities p_ent INNER JOIN entities c_ent ON p_ent.id = c_ent.parent_id WHERE c_ent.attribute_id = ?
But I do not know how to do this in the Doctrine.
Of course I tried:
$qb = $em->createQueryBuilder();
$qb->select('p_ent', 'c_ent')
->from('Entities', 'p_ent')
->innerJoin('p_ent.children', 'c_ent')
->where('c_ent.attribute = ?1')
->setParameter(1, $attr);
But this will not work - relationships are always loaded completely, with all the child objects.
source
share