Join queries in Doctrine on tables without specifi

2019-08-10 11:33发布

问题:

I have two tables that do not have a relation defined with each other in the schema.yml. However, table 1 has a foreign key reference to the primary key of table 2. Clearly, I goofed up by not designing the database well, but now it's mitigation time.

I must do a left join between the two tables coupled with a where clause that will retrieve the select rows I want. And to do this, I do:

 Doctrine_Query::create()->select('t.*, l.lid')->from('Taxonomy t')->leftJoin('t.Cid c')      ->leftJoin('c.Lesson l')->where('t.section = ?','Critical reading');

This should typically do it, but it does not because what it returns is all the rows from taxonomy table irrespective of the where condition. I am thinking, is this because of the relation not being specified in the column? That would be ridiculous cause the query works, only in a doctrine context it does not.

Thanks

回答1:

In doctrine you can only join using the relations you defined on your schema, this is a know limitation. You may use the Native SQL feature as a workaround.