H
H
Herman Martin2018-01-31 21:59:14
Yii
Herman Martin, 2018-01-31 21:59:14

Error Trying to get property 'name' of non-object ->with() how to solve?

I have a query like:

$query = Event::find()->where(['i_user' => $_SESSION['user']['id']])
                    ->with('category')
                    ->with('type')
            ->orderBy($rsort2)

in model:
public function getCategory() {
        return $this->hasOne(Category::className(), ['id' => 'i_cat']);
    }
    public function getType() {
        return $this->hasOne(Type::className(), ['id' => 'type']);
    }

When outputting, the error described in the topic occurs:
<?=$ev['category']->name?></td>
    <?=$ev['type']->name?></span>

moreover, the output of the category name works without errors, but the output of the type name does not see, although the sampling functions are similar. There was an idea to just work with arrays:
$query = Event::find()->where(['i_user' => $_SESSION['user']['id']])
                    ->with('category')
                    ->with('type')
            ->orderBy($rsort2)
            ->asArray()
            ->all();

but there is also pagination in the code
Any ideas?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
VelAnna, 2018-01-31
@dklight

Try getting data like this:

<td><?= $ev->category->name ?></td>
<span><?= $ev->type->name ?></span>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question