Oct
2
When Doctrine leftJoin fails
Filed Under PHP, Symfony | Leave a Comment
I’ve stumbled across a weired problem in Symfony, using Doctrine. Whenever I wanted to use a leftJoin, stuff would fail. So, here are some tips to help you pass this problems:
1. Check that the leftJoin is issued like $q->(a.RelationshipName r);
where “a” is the root alias of the query, RelationshipName is the NAME of the relationship defined in the yaml file of the database.
2. Make sure you use the actual relationship name from the yaml DB schema, and not the class name or something else. For instance, let’s say you have 2 classes: MysiteArticle and MysiteCategory. To fetch articles, you would build your query like: Doctrine_Query::create()->from(’MysiteArticle a’);
But, to fetch articles together with the category, you would do something like:
Doctrine_Query::create()->from(’MysiteArticle a’)->leftJoin(’a.Category c’); if your relationship name is “Category”, in the schema.yml, or Doctrine_Query::create()->from(’MysiteArticle a’)->leftJoin(’a.MysiteCategory c’); if your relationship name is MysiteCategory.