V
V
Veronika Stepanovna2020-07-08 11:12:07
Yii
Veronika Stepanovna, 2020-07-08 11:12:07

What is the inappropriate relationship behavior in yii2?

There is this relationship:

public function getContractor()
    {
        return $this->hasOne(Contractors::class, ['id' => 'contractor']);
    }


when I try to get it to
$model->contractor

me instead of the model, the model returns int ID,

I kind of carefully read the doc. what's wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim, 2020-07-08
@sargss

Attentively, but inattentively, they made the name of the properties or relationships.
You have a property in the database and in the model that matches the name of the connection. Therefore, you need to change it to contactor_idor change the name of the connection. getContactorRelation
On request $model->contractor, you get an id. First, the property is searched, and then the relationship.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question