I have an Article model and a Photo model, and I am currently using a query like this to join the two:
$q = Doctrine_Query::create()
->from('Article a')
->leftJoin('a.Photo p');
For binding them together, I have an ArticlePhoto model. In addition to article_id and photo_id fields, the model also has a priority INT field, with which I want to sort the Photos in the above query.
Any suggestions how I can achieve this, without having to join through a query like this:
$q = Doctrine_Query::create()
->from('Article a')
->leftJoin('a.ArticlePhoto ap')
->leftJoin('ap.Photo p')
->orderBy('ap.priority');
There is a lot of code expecting to find the first photo for the article in $article['Photo'][0], so I want to avoid having to change this. Thanks for any suggestions!
What’s wrong with the joins? You can always rewrite your query to this:
Which looks like a perfectly fine query without any magic. You need information from the ArticlePhoto table (for sorting), so doing an explicit join is the right thing to do.