Doctrine relation betwen two tables
I have two tables A and B. A have one or more registers in B. I want to join two tables and extract all information about A.
I do that:
Schema.yml
A:
fields.....
B:
fields...
relations:
A: { onDelete: CASCADE, local: a.id, foreign: b.id, foreignalias: AB }
And i try开发者_如何学编程 to do that...
$q = Doctrine_Query::create()
->from('A a')
->leftJoin('a.AB b')
->where('a.field = "D"')
->andWhere('b.codzon = ?', $this->cp);
It prints me error: Unknown relation alias AB What i am doing wrong?
Your query seems to be ok, errors are in you sheme definition. You need to define relations on both sides: A and B. I am not very comfortable with YML, but it should look like this:
A:
relations:
B:
local: id
foreign: id
foreignAlias: AB
foreignType: many
type: one
See also http://www.doctrine-project.org/documentation/manual/1_0/ru/yaml-schema-files#relationships:one-to-many for detect_relations option.
Remove table names from schema:
...
A: { onDelete: CASCADE, local: id, foreign: id, foreignalias: AB }
...
精彩评论