Attempt to assign property of non-object

Hi Gang !!

I have this function and have some problems I dont understand

it throws the above error

The print_r($this->orderItems); prints out an array of objects but then inside the foreach loop its not present the print_r($orderItem); prints out ‘a2’ ?

and the the next line $orderItem->orderid = $this->id; throws the error.

What am I missing here ??




	protected function afterSave() { 

                parent::afterSave();

                print_r($this->orderItems);

                foreach($this->orderItems as $orderItem) {

                		//print_r($orderItem);

                		//exit();

                        $orderItem->orderid = $this->id;

                        $orderItem->save();

                }

                //return parent::afterSave();

                

   	}



Try this syntax




foreach($items as $key=>$value)

  echo $value;



Maybe the field is not loaded.

Could be selected only some field in "select statement" in CDbCriteria.

Same result it prints out ‘a2’ ? this is weird the print_r($this->orderItems); just above the foreach loop prints out the array of objects just fine.

I dont quiet follow could you please explain a bit further ?

You should check if you are trying to access a attribute that wasn’t loaded in “select statement”.

Internaly if you select if SELECT *, so all field are returned.

Maybe this is not the problem, i can’t say for sure.

Ok this is how it is done it is new records being created first I create an Order object and then add orderitems to it, teh error is thrown when I am trying to save the order object, and also the aftersave function belong to the order class.

So no data is actually coming from the database in the above example just new objects I am trying to save to the database.

Could you show me: print_r($this->orderItems); ?