Trying To Get Property Of Non-Object

Hi,

Can anyone please tell me why i am getting ERROR:"Trying to get property of non-object" when i am executing below mentioned code:

Controller(Restaurant):

	 $restaurants = $model->findAll($condition);


	 $merchant = $restaurants->merchant;


	 $arr = array();


	 foreach($restaurants as $t)


	 {


		$arr[$t->id] = $t->attributes;


	 }


	 print_r($arr);

[u][b]

Relations are[/b][/u]

Model(Restaurant):

public function relations()


{


	// NOTE: you may need to adjust the relation name and the related


	// class name for the relations automatically generated below.


	return array(


	'merchants'=>array(self::BELONGS_TO,'Merchant','merchant_id'),


	);


}

Model(Merchant):

public function relations()


{


	// NOTE: you may need to adjust the relation name and the related


	// class name for the relations automatically generated below.


	return array(


		'restaurants'=>array(self::HAS_MANY,'Restaurant','merchant_id'),


	);


}

findAll returns an array (line 1) and you’re assuming it is a single model (line 2 in your code).

Also, FYI such discussions suit more the ‘general questions’ forum, not this one.

Remove this line

$merchant = $restaurants->merchant;

Thanx for your suggestion.I understand that i have to Write “$merchant = $restaurants->merchant;” this line in the foreach loop but can’t figure out the actual syntax can you help me in it.

And please tell me what i have to do if i try to implement this code with eager loading approach.

use $merchant = $restaurants->merchant; inside the foreach loop. if it needed.

like $merchant = $t->merchant;

right. and do check if its needed at all… .

But i need merchant name for each restaurant how can i get it.please advice.

I removed "$merchant = $restaurants->merchant;"

And i am getting array like:

Array

(

[3] => Array


    (


        [id] => 3


        [merchant_id] => 1


        [name] => BRestaurant One


        [address] => BTest Address One


        [website] => www.bwebsiteOne.com


        [created_date] => 2012-12-14 11:48:19


        [modified_date] => 2013-01-08 06:05:20


        [created_user] => 1


        [isblocked] => N


        [isdeleted] => N


    )

)

i need to get the merchant name can you please tell me what should i do

when i use this code in loop it is returning ERROR:"Property "Restaurant.merchant" is not defined."

Information abbout "merchant" is in other table (i suspect)

So you need do have relation in Restaurant for table where is information "merchant";

exemple:

public function relations() {

      return array(


        'rMerchant' => array(self::BELONGS_TO, 'merchant', 'merchant_id'),





}

you have this relation, only names are different

soo you can data retrive like $merchant = $restaurants->rMerchant->merchant;

but you have to add this in foreache (

already someone say => findAll return array)

[color="#006400"]/* Moved from "Yii-powered Applications" to "General Discussion for Yii 1.1.x" */[/color]

first check that value is set or not like

if(isset($t->attributes))

{

// your code here

}