Filter AR query by related AR attribute

I will try to explain what I need but I’m not sure if I can do this since english is not my first language… :)

I have the following AR classes:

Quote

class ProcessoSelecaoCandidato extends CActiveRecord

{

    …

    public function relations()

    {

    return array(

    'aluno'=>array(self::BELONGS_TO, 'ProcessoSelecaoPessoa', 'id_pess_aluno', 'aliasToken'=>'pessoa'),

    'responsavel'=>array(self::BELONGS_TO, 'ProcessoSelecaoPessoa', 'id_pess_resp'),

    );

    }

    …

}

Quote

class ProcessoSelecaoPessoa extends CActiveRecord

{

    …

}

What I need is to return all the records of ProcessoSelecaoCandidato based on some filters (check this topic) and one of the filter is an attribute of ProcessoSelecaoPessoa, wich is a related class of ProcessoSelecaoCandidato.

I can't use the option 'condition' in relationship declaration because the filter is dynamic.

For example, student name like ‘steve’ and name is an attribute in ProcessoSelecaoPessoa.

My last attempt was trying to use 'aliasToken'=>'pessoa' in relationship declaration and try to get the result like this:

Quote

$criteria=new CDbCriteria;

$criteria->condition = "pessoa.nome LIKE '%$nameFilter%'";

$processoselecaocandidatoList=ProcessoSelecaoCandidato::model()->with('aluno')->findAll($criteria);

But all I get is:

Quote

CDbCommand failed to execute the SQL statement: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'pessoa.nome' in 'where clause'

Does this have a solution?

Unfortunately, AR doesn't have an easy way to do this. You would need to use findAllBySql() or set CDbCriteria's 'join' attribute to achieve this.

I didn't notice the CDbCriteria's 'join' attribute!

It saved my day!

Thank you again!