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:


  'join' => 'JOIN GrandChild ON =', 

  '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



  'childs.grandchilds' => array(

    'select' => false,

    'condition' => 'grandchilds.gender = "MALE"',




Try this instead (no need to create model and relatonship)


  'childs' => array(

    'join' => 'JOIN GrandChild ON =', 

    'condition' => "GrandChild.gender = 'MALE'",




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