Named Scope With Relation

Hello,

I’ve been struggling with this for a few days. I’ve seen other people use a similar approach and, although there is a known bug related to applying multiple scopes, it seems to work for them. Check this “With” declaration inside various scopes post for reference.

I have 3 models A, B and C abstracting access to database tables a, b and c respectively. A BELONGS_TO B and B HAS_MANY C.

My goal is to find all instances of A that result of applying a scope defined in B that in turn depends on C.

And the problem is that C remains "hidden" to A, even when the "path" to reach it is defined along the three models:




class A extends CActiveRecord {

    ...

    public function relations() {

        return array(

            ...

            'b' => array(

                self::BELONGS_TO,

                'B'

            )

        );

    }

}






class B extends CActiveRecord {

    ...

    public function relations() {

        return array(

            ...

            'cs' => array(

                self::HAS_MANY,

                'C'

            )

        );

    }

    public function b_scope($param) {

        $this->getDbCriteria()->mergeWith(array(

            'with' => array(

                'cs' => array(

                    'joinType' => 'inner join'

                )

            ),

            'condition' => 'cs.x = :param',

            'params' => array(

                ':param' => $param

            )

        ));

        return $this;

    }

}






class C extends CActiveRecord {

    ...

}



Now if I try:




    A::model()->with(array(

        'b' => array(

            'scopes' => array(

                'b_scope' => $x

            )

        )

    ))->findAll();



I get an error:




Undefined table: 7 ERROR: missing FROM-clause entry for table c



I have tried many variants, like putting the conditions inside the scope, and playing with all possible options with no success.

Any help will be appreciated.