开发者

Eager loading a partial association in Doctrine

I want to fetch some entitie开发者_如何转开发s with a part of their associations eager loaded.

It is a self-referential hierarchical relationship, with a table like such:

CREATE TABLE entities (
    id serial PRIMARY KEY,
    parent_id integer REFERENCES entities,
    attribute_id integer REFERENCES attributes,
    [...]
);

Now I want to preload an entity with some of its children, matching a WHERE clause like entities.attribute_id = ?.

I have this in plain 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 don't know how to do this in 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 doesn't work -- the relationship always gets loaded fully, with all the children entities.


try this :

$qb = $this->_em->createQueryBuilder()
        ->select('PARTIAL p_ent.{id, your_fields ...}')
        ->from($this->_entityName, 'p_ent')
        ->innerJoin('p_ent.children', 'c_ent')
        ->addSelect('PARTIAL c_ent.{id, your_fields ...}')
        ->where('c_ent.attribute = ?1')
        ->setParameter(1, $attr);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜