Yii Framework Forum: Relation through middle table - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Relation through middle table Table A has a relation to table B, table B has a relation to table C Rate Topic: -----

#1 User is offline   hutch90 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 14-February 18

Posted 16 May 2018 - 06:22 PM

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 ?????
            'c' => array('???', 'C', '???')
        );
    }
}

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

0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users