开发者

Hydrate doctrine collection with another model than the FROM clause

On my application, i frequently do doctrine query like this :

$coms = Doctrine_Core::getTable('Comment')
->createQuery('c')
->join('c.article a')
->join('a.Writter w')
->where('w.something = ?', $something); 

I want to extract the comments from articles with a condition on the writter. Le query start from the Comment Table ( because finally, we want a doctrine collection of Comment ), next i join to all the articles and then to all the writters. Finally, i make a restriction with the condition on the writters.

This query could be more optimized with the joints in this order :

$coms = Doctrine_Core::getTable('Writter')
->createQuery('w')
->select('c.*')
->join('w.Article a')
->join('a.Comments c')
->where('w.something= ?', $somethin开发者_如何学运维g); 

Like this, the number of lines manipulated by the joints is much more reduced., because the restriction on the writter is made at first.

But with this code, i got an error :

The root class of the query (alias w) must have at least one field selected.

Does a solution exist to keep this order in joints and obtain finally a doctrine collection of Comment ?


you should simply add the the joined table to the select (as Doctrine tells you in the error message btw):

->select('c.*, w.*')


no there is no other solution than to keep your first query. You always get a collection of object of the root class you choose by using getTable('xyz'). There is no way around this.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜