Yii Framework Forum

Relation through middle table


(Jeff) #1

I have three tables that are related. Table A has a relation to table B, and table B has a relation to table C. Is there a way to get from A to C with a single relation? I know that there is a way if a many-to-many table is involved, but that’s not the case here. Instead, multiple entries from A can match with a given entry from C, but only one entry from C can match with a given entry from A.

For example, I could get to B from A with something like $a->b using a relation using the provided example code, but how can I do $a->c instead of $a->b->c?




class A extends ActiveRecord

{

    public function relations()

    {

        relations = array(

            // $a->b

            'b' => array(self::BELONGS_TO, 'B', 'b_id'),

            // $a->e

            'e' => array(self::MANY_MANY, 'E', 'd(a_id, e_id)'),

            // $a->c <img src='https://www.yiiframework.com/forum/public/style_emoticons/default/huh.gif' class='bbc_emoticon' alt='???' />??

            'c' => array('<img src='https://www.yiiframework.com/forum/public/style_emoticons/default/huh.gif' class='bbc_emoticon' alt='???' />', 'C', '<img src='https://www.yiiframework.com/forum/public/style_emoticons/default/huh.gif' class='bbc_emoticon' alt='???' />')

        );

    }

}


class B extends ActiveRecord

{

    public function relations()

    {

        relations = array(

            // $b->c

            'c' => array(self::BELONGS_TO, 'C', 'c_id')

        );

    }

}


$a->b->c; // What I can do, but don't want to do

$a->c; // What I want to do, but don't know how to do