Fatal Error: Call To A Member Function Of A Non Object

Hi, anyone can help me?

I got stuck of this code.




$billing->bookingHeader->getCodeNumber('BKG');

Actually $billing is a model and bookingHeader is the relation.

this line generate error


Fatal error: Call to a member function getCodeNumber() on a non-object

Below is the component contains result.


class MonthlyTransactionActiveRecord extends ActiveRecord

{


	public function getCodeNumber($cnConstant = '')

	{

		$arr = array('I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX', 'X', 'XI', 'XII');

		$month = $this->cn_month ? $this->cn_month - 1 : 0;

		return sprintf('%02d/%04d/%s/%s/%02d', $this->hotel_id, $this->cn_ordinal, $cnConstant, $arr[$month], $this->cn_year);

	}

}

I call




$billing->bookingHeader->getCodeNumber('BKG');

twice (in view and controller), it has no error in view, but when I call it in Controller, it gives me error.

What kind of trigger the error has? How to fix that?

Thank you…

It seems to be that “[size=2]bookingHeader” is not set properly. Do you have “bookingHeader” in billing model’s relations array?[/size]

relations are lazy loaded, and if there is no related object the relation value is null. so…

when you call $billing->bookingHeader, Yii tries to find ‘MonthlyTransactionActiveRecord’ model in database whioch is related to your $billing, and if it does not exists $billing->bookingHeader == null. This is why you get “Call to a member function getCodeNumber() on a non-object”. You should check if your related object exists or ensure it does other way.

One more thing - lazy load caches the result, so if try to get related model all subsequent call will use that first returned value.