I have models: A
[id, value]
, B
[id, value]
and X
[id, a_id]
.
I want to find on X
contain A
and contain B
where B.value = A.value
, both A
and B
value
fields are unique, so there is only one row relation (if any).
$this->X->find('all', array(
'contain' => array(
'A' => array('B')
)
));
What i tried was $belongsTo
association (on both side)
'B' => array(
'foreignKey' => false,
'conditions' => array(
'A.value = B.value'
)
)
In sql log there is only 1=1
in WHERE
section.
Is there a possibility to solve it without using joins
, in single query?
You have to use
For cakephp 3.x
As mentioned in http://book.cakephp.org/3.0/en/orm/associations.html
For cakephp 2.x
As mentioned in http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html
This will work for you
Also note that the default recursive level is 1 so you have to define the recursion to 2 as mention in this doc http://book.cakephp.org/2.0/en/models/model-attributes.html#recursive