I will try to explain the essence of the issue. I have 3 tables: items , rows and item_prop .

The items table is a list of objects (id key), rows is a list of properties that an object can have (id key), item_prop is a table that links items and rows and stores the value. That is, it has the following fields: item_id, row_id, value. Here I think it is clear.

Actually the question itself: how correctly in the model to describe these links, what would GridView work correctly, namely, search and sorting. I only know when 2 tables are connected (via hasOne or hasMany), but I don’t know how.

Please tell me or poke an example. thank

1 answer 1

For example, from the table Props take row_id and prop_id.

class SomeClass extends Smth { public function getItem() { return $this->hasOne(Item::className(), ['id' => 'item_id']); } public function getRow() { return $this->hasOne(Props::className(), ['id' => 'row_id']); } public function getProp() { return $this->hasOne(Props::className(), ['id' => 'prop_id']); } } 

And we take what we need:

 $props = Props::find()->with('item, row, prop')->all(); foreach ($props as $prop) { $item = $prop->item; $row = $prop->row; $prop = $prop->prop; } 

ps with yii2 worked for a long time, but it can be done somehow, like,