S
S
Sergey Zhukov2015-10-02 11:49:44
Yii
Sergey Zhukov, 2015-10-02 11:49:44

How to make connections through trhough in Yii for 4 tables?

I struggled for half a day) I understand that you can make a SQL query. And I wrote it:

SELECT
              DISTINCT pavT.name
            FROM
              Buildings AS b
            JOIN Pavilions AS p ON p.building_id = b.id
            JOIN PavilionsTypes AS pt ON p.id = pt.pavilion_id
            JOIN PavTypes AS pavT ON pt.type_id =  pavT.id
            AND b.id = 1

But it would be desirable to make through relations (relation)
The task that I address to one Building and I receive all Types of the goods which are on sale there.
I have Buildings (buildings) that have many Pavilions (pavilions). Each pavilion can have many PavTypes (Types of goods sold there), via the PavilionsTypes table ( MANY_MANY )
bfbe8f5593b142f0abd1cc521e292f60.png
class Buildings extends CActiveRecord
{
  public function relations()
  {
    return array(
      'pavilions' => array(self::HAS_MANY, 'Pavilions', 'building_id'), //тут все очевидно
      'actualPavTypes' => array(self::HAS_MANY, 'PavTypes', 'building_id', 'through' => 'pavilions'),
    );
  }
}

Those. due to actualPavTypes , I expect to get a bunch of PavTypes

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question