Q:

Yii CDbCriteria select a field from a related object from another related object

Hi I need to create a CDbCriteria query with more complex relations.
Table1 has a relation HAS_ONE to Table2 - let's call it Relation1
Table2 has a relation HAS_ONE to Table3 - let's call it Relation2
Table3 has my desired Field let's call it Field2
$this refers to the class of Table1, where I defined 
class Table1 extends ActiveRecord {

    public $Field1; // so I can do a search on it

    ...

    public function mySearch()
    {
        $criteria = new CDbCriteria;

        //I need to do sth like this:
        $criteria->compare('Relation1.Relation2.Field2',$this->Field1);

        return new CActiveDataProvider($this, array(
            'criteria'=>$criteria,
        ));
    }
}

But with this I get an error, 
Column not found: 1054 Unknown column 'Relation1.Relation2.Field2' in 'where clause' 
Any ideas how to fix it? I do not want to write my own join, I want to do it the ORM way.

A:

Use this:
class Table1 extends ActiveRecord {

    public $Field1;

    public function mySearch()
    {
        $criteria = new CDbCriteria;
        $criteria->with = array(
             'relation_to_table2', 
             'relation_to_table2.relation_to_table3'
        );
        $criteria->compare('relation_to_table3.Field2',$this->Field1);

        return new CActiveDataProvider($this, array(
            'criteria'=>$criteria,
        ));
    }
}

