I have 3 entities say Parent, Child and GrandChild, they are related according to their names.
GrandChild has an attribute gender.
Now I what to retrive all Parents with Childs but only if Parent has a GrandChild of gender male. I do not need to retrieve and of GrandChilds.
I try to do something like this:
Parent::model()->with('childs')->findAll(
'join' => 'JOIN GrandChild ON childs.id = GrandChild.id',
'condition' => "GrandChild.gender = 'MALE'",
);
The problem is that in generated SQL join' part comes before any of autogenerated
LEFT JOINs’ and I cannot reference them.
Are there any workarounds?
tri
(tri - Tommy Riboe)
2
One solution may be:
Define a relationship grandchilds in the childs class
Parent::model()->with(array(
'childs',
'childs.grandchilds' => array(
'select' => false,
'condition' => 'grandchilds.gender = "MALE"',
),
)->findAll();
/Tommy
tri
(tri - Tommy Riboe)
3
Try this instead (no need to create model and relatonship)
Parent::model()->with(array(
'childs' => array(
'join' => 'JOIN GrandChild ON childs.id = GrandChild.id',
'condition' => "GrandChild.gender = 'MALE'",
)
))->findAll();
/Tommy
chamara
(Bandara Chamara)
4
how to add a condition to parent table like filter by id = 2 