T
T
Tokenchik2017-05-28 16:02:51
Yii
Tokenchik, 2017-05-28 16:02:51

How to get all fields of their related table in yii2?

How to get rest of fields from related table in yii2?
Right now I am getting one field like this -

public function getBase() {
        return $this->hasOne(Base::className(), [ 'id' => 'base_id' ]);
    }

    public function getIp() {
        return $this->base->ip;
    }

As you can see, the connection goes by the id of the base, I get the ip with the getIp function. It turns out for each field such function is necessary?
And how can I be allowed if I need the name field from the second table, despite the fact that there is a name field in this table as well?
After all, I specify the field received in the connection as an attribute to give it a label in the grid.
PS The above code is working, I get the required field.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Timofeev, 2017-05-28
@Tokenchik

As you can see, the connection goes by the id of the base, I get the ip with the getIp function. It turns out for each field such function is necessary?

No, and this is not required. It is possible like this:
$models = Mymodel::find()->all();
foreach($models  as $one){
if(isset($one->base)){
echo $one->base->ip;
echo $one->base->name;
echo $one->base->eshe_chto-to;
}
}

I will even say more, there is very little sense in your getIp () function, if only like this:
public function getIp() {
        return ($this->base)?$this->base->ip:"нет данных";
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question