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

there is a query of the form:
$query = Event::find()->where(['i_user' => $_SESSION['user']['id']])
->with('category')
->with('type')
 ->orderBy($rsort2)

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

when withdrawal occurs as described in the topic error:
<?=$ev['category']--->name?>
 <?=$ev['type']--->name?>


moreover, the output name category fulfills without errors, and the output type name is not see, though the function of the selection is similar. The idea was just to work with arrays:
$query = Event::find()->where(['i_user' => $_SESSION['user']['id']])
->with('category')
->with('type')
->orderBy($rsort2)
->asArray()
 ->all();

but in the code there is still pagination

Any ideas?
June 8th 19 at 17:08
2 answers
June 8th 19 at 17:10
Solution
Try to get the data like this:
<?= $ev--->category->name ?>
<span><?= $ev--->type->name ?></span>
comes out the same error. If you do
<span><?= $ev--->type ?></span>
displays the value of the first column of the table, namely id - Afto commented on June 8th 19 at 17:13
and where did you get the variable $ev? In the above code, it is not.
I understand what you are trying display the data in gridView? - citlalli65 commented on June 8th 19 at 17:16
I can't see your code, try to iterate using foreach
foreach($ev as $item) {
 echo $item->type->name."<br>";
}
- citlalli65 commented on June 8th 19 at 17:19
$query = Event::find()->where(['i_user' => $_SESSION['user']['id']])
->with('category')
->with('types')
->orderBy($rsort2)
//->asArray()
//->all();
;
 //echo Debug::d($query,'query'); die;
 $q_counts = 10;
 $pages = new Pagination(['totalCount' => $query->count(),'pageSize' => $q_counts,
 'pageSizeParam' => false, 'forcePageParam' => false]);
 $events = $query->offset($pages->offset)

 // next 
 foreach ($events as $ek => $ev): 
...

<?=$ev--->types['color']?>
<?=$ev--->types->name?>

...
 endforeach;

with a variety of options to get the value, for example: $ev->['types']['color']

solved the problem by changing the ->with('type') on ->with('types')

Thanks for the tip - Afto commented on June 8th 19 at 17:22
June 8th 19 at 17:12
Solution
Preamble:
Class Event the heir to ActiveRecord.
When you access a property, if your class within PHP is not declared available property type - it calls the magic method __get('type') (the parent class) to determine the existence within Yii attribute, or a getter getType() to retrieve the value of this attribute: yii\db\BaseActiveRecord::__get()
- First checks for the existence of an attribute with the same name, if available - returns the value of the attribute.
- Then checks for the existence of the already obtained values for the associated data. It is usually the other object is the heir of ActiveRecord in your case as described for the class Type.
- If is not something is not found, it checks for a getter and calling it, for example getType().
- If the getter returned a non-query object data, it returns the value from the getter.
Methods for determining relationships (hasOne, hasMany, etc.) return an object of type ActiveQueryInterface (usually it ActiveQuery).
- If the result of the getter returns an object of type ActiveQueryInterface, the query is executed, and then returns the query result.
Thus we get the result of the query and get the related record or records ActiveRecord.
If you want to obtain the result of the query related data (an object of type ActiveRecord), then call $ev->type, but if the query itself for further manipulation, then $ev->getType().

Answer:
In your case, the error that is described in to getType() of when the case comes. Because there is a type attribute.
Your code $ev->type - returns "aydishnik". And you turn to him then as to the object ...->name.

1. You want to call the link and the attribute of the storage connection.
It is more convenient to do so - an attribute called type_id (in the database and in the rules):
public function getType() {
 return $this->hasOne(Type::className(), ['id' => 'type_id']);
}

And then:
- $ev->type_id - aydishnik related data.
- $ev->type - thanks to the magic __get() and the description of the communication via getType() - returns the associated object of type class Type (it inherits from ActiveRecord).
- $ev->getType() - request ActiveQuery to get related data.

2. Or rename getType() on something like getEventType(), and then it will work out $ev->eventType->name.
Thank you, Your answer is full and detailed, solved the problem change ->with('type') on ->with('types')
and this code:
public function getType() {
 return $this->hasOne(Type::className(), ['id' => 'type']); // instead of the more correct ['id' => 'type_id']
}

left due to design errors.

Thank you! - Afto commented on June 8th 19 at 17:15

Find more questions by tags Yii