A
A
alex4answ2019-03-19 08:49:23
Yii
alex4answ, 2019-03-19 08:49:23

How to correctly build a relationship between 3 tables?

Hello, there are 3 tables:

elements - Список элементов
permission - список разрешений для элементов
per_tariffs - список разрешений для тарифов пользователей

This is what the schema looks like:
5c9081fa8044f536345136.png
What is the actual problem:
I'm trying to get a list of items along with permissions.
At the beginning, I only have elements, and I'm trying to do this:
получаю permission по elements.type, затем получаю per_tariffs по permission.id

It all works if you manually compose an sql query, but I have an active record, and I want to figure out how to do such things with it, now I'm trying to do this:
В моделе Elements:
public function getPermission(){
        return $this->hasMany(Permissions::className(), ['type' => 'type'])
                ->viaTable('per_tariffs', ['per_id' => 'id']);
    }

В контролере :
$model = Elements::find()
                ->with('permission')
                ->asArray()
                ->all();
.. debug($model);

But permission is empty in the model comes out.
Please tell me how to do it right? I just feel like a ram rested and I’m looking in the wrong direction, somehow this should just be solved (

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