Answer the question
In order to leave comments, you need to log in
Yii Framework: why does PHP notice Trying to get property of non-object. In one case it works, in the other it doesn't. Is the code identical?
Good afternoon.
I can't figure out what's wrong. I pass data from two related tables to the GridView, everything works with one table, but not with the second.
CODE IN MODEL
public function getType(){
return $this->hasOne(Type::className(), ['id' => 'id_type']);
}
public function getUser(){
return $this->hasOne(User::className(), ['id' => 'id_user']);
}
<?= GridView::widget([
'dataProvider' => $dataProvider,
'tableOptions' => ['class' => 'table table-striped'],
'columns' => [
'id',
'created_at',
'id_user',
[
'attribute' => 'id_type',
'value' => function($data){
return $data->type->name;
},
],
'client',
'title',
[
'attribute' => 'id_user',
'value' => function($data){
return $data->user->name;
},
],
'deadline',
'price',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
Answer the question
In order to leave comments, you need to log in
Check through try on which user_id the application crashes. You can also output like this and everything will be clear.
return !empty($data->user) ? $data->user->name : 'empty';
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question