L
L
Leopandro2016-07-08 16:13:50
Yii
Leopandro, 2016-07-08 16:13:50

How to get model with hasOne multiple times?

There is a model $panel it has a function

public function getCar()
    {
        return $this->hasOne(Car::className(), ['id' => 'car_id']);
    }

car has:
public function getBrand()
  {
    return $this->hasOne(Brand::className(), [
      'id' => $this->brand_id
    ]);
  }

Accordingly, to get the $name attribute from $brand, I use the following code:
$model->car->brand->name
Displays an error:
Getting unknown property: app\models\Car::4

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Timofeev, 2016-07-08
@Leopandro

change

public function getBrand()
  {
    return $this->hasOne(Brand::className(), [
      'id' => $this->brand_id
    ]);
  }

on the
public function getBrand()
  {
    return $this->hasOne(Brand::className(), [
      'id' => 'brand_id'
    ]);
  }

since this is a link, you must specify the name of the column in the database, and not its value

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question