Trying to get property of non-object

hello,

I want to get a hint from the following message in the guide:

view.php


CHtml::encode($model->regions->name)

it has no problem when fetching the field from another table.

_view.php


CHtml::encode($data->regions->name)

it displays the error messages as follows:

i think this is the problem i didn’t loop. help me…

Hi,

  1. You need to check if $data->regions exists :



<?php if (!empty($model->regions)):?>

   <?php echo  CHtml::encode($model->regions->name);?>

<?php endif;?>



  1. Pls, check if you really put into _view ($data) the same variable as in view.php ($model)

both $data and $model variables exist, so I var_dumped two variables:

_view.php


<?php var_dump($data->regions); ?>

view.php


<?php var_dump($model->regions); ?>

the difference is as follows:




$ diff view _view 

1c1

< object(Region)#72 (12) {

---

> object(Region)#83 (12) {

3c3

<   object(CActiveRecordMetaData)#63 (5) {

---

>   object(CActiveRecordMetaData)#74 (5) {

5c5

<     object(CMysqlTableSchema)#64 (9) {

---

>     object(CMysqlTableSchema)#75 (9) {

22c22

<         object(CMysqlColumnSchema)#65 (14) {

---

>         object(CMysqlColumnSchema)#76 (14) {

53c53

<         object(CMysqlColumnSchema)#66 (14) {

---

>         object(CMysqlColumnSchema)#77 (14) {

92c92

<       object(CMysqlColumnSchema)#65 (14) {

---

>       object(CMysqlColumnSchema)#76 (14) {

123c123

<       object(CMysqlColumnSchema)#66 (14) {

---

>       object(CMysqlColumnSchema)#77 (14) {

157c157

<       object(CHasOneRelation)#67 (18) {

---

>       object(CHasOneRelation)#78 (18) {

202c202

<     object(Region)#62 (12) {

---

>     object(Region)#73 (12) {



It seems like there’s no difference as an object describing relationship. However, it still complains that I’m trying to get a property of non-object.

You’re correct. Not all variables had values… some of them were null. That’s the reason why I failed. Thank you for your help.