Yii Framework Forum: Trying to get property of non-object - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Trying to get property of non-object Rate Topic: -----

#1 User is offline   jonghwa 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 19
  • Joined: 04-December 11
  • Location:Seoul, Korea

Posted 05 December 2011 - 12:45 AM

hello,

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

Quote

you will need to loop through the results before trying to access any properties. Otherwise, you may receive "Trying to get property of non-object" errors.


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:

Quote

Trying to get property of non-object


i think this is the problem i didn't loop. help me...
0

#2 User is offline   Weavora Team 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 141
  • Joined: 06-December 10

Posted 05 December 2011 - 12:51 AM

Hi,

1. You need to check if $data->regions exists :
<?php if (!empty($model->regions)):?>
   <?php echo  CHtml::encode($model->regions->name);?>
<?php endif;?>


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

#3 User is offline   jonghwa 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 19
  • Joined: 04-December 11
  • Location:Seoul, Korea

Posted 05 December 2011 - 01:30 AM

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.
0

#4 User is offline   jonghwa 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 19
  • Joined: 04-December 11
  • Location:Seoul, Korea

Posted 05 December 2011 - 01:49 AM

View PostWeavora Team, on 05 December 2011 - 12:51 AM, said:

Hi,

1. You need to check if $data->regions exists :
<?php if (!empty($model->regions)):?>
   <?php echo  CHtml::encode($model->regions->name);?>
<?php endif;?>



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.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users