A
A
AR2017-01-06 06:04:36
Yii
AR, 2017-01-06 06:04:36

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']);
    }

CODE AS
<?= 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'],
        ],
    ]); ?>

In the case of getting data from Type, everything is fine, but from User it issues Trying to get property of non-object.
What can be wrong? The code is absolutely the same.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
melnikov_m, 2017-01-06
@amio

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 question

Ask a Question

731 491 924 answers to any question