Parent::model()->with('childs')->findAll('join' => ...): cannot reference `childs' relation in `join'

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 autogeneratedLEFT JOINs’ and I cannot reference them.

Are there any workarounds?

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

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

how to add a condition to parent table like filter by id = 2 ???