I have a user practice model and a car model. In the occupation model, the auto field is associated with the id in the auto model. In order to make it easier for me to select these cars during class output, I wrote this in the class model:

public function getAuto() { return $this->hasOne(Autos::className(), ['id' => 'auto']); } 

And when outputting I use this:

 var_dump($model->auto) 

But nothing is displayed. Am I doing something wrong? Sorry if I explained poorly

  • I got it, thank you for your help. the fact is that I have the same auto field, and the method is also auto, and by calling $ model-> auto, I did not receive an auto object, but only its id. changed to getAutos () - everything works - Diefair

1 answer 1

I will answer myself, it may be useful to someone: the methods for obtaining related data should not be named the same as fields. In my case, I have an auto field and a getAuto method that I call as

 $model->auto 

thereby obtaining unrelated data, but only their id