"Trying to get property of non-object" result from Conditional belongs_to relations

Hi,

I have 3 models,"Order", "OrderItem" and "Plan"

Order:




public function relations()

{

	return array(

		'orderItem'=>array(self::HAS_MANY, 'OrderItem', 'orderId'),

	);

}



OrderItem has a field “type”, whoes value could be “plan” or “value add service”. If it’s “plan”, then I want to retrieve Plan model by define the following relations.

OrderItem:




public function relations()

{

	return array(

		'plan'=>array(self::BELONGS_TO,'Plan','productId','condition'=>'OrderItem.type="plan"','join'=>'LEFT JOIN OrderItem ON plan.planId = OrderItem.productId'),

	);

}



However, if an Order has some orderItems, and some’s type is not “plan”, I’ll get “Trying to get property of non-object” error with the following code




foreach($order->orderItem as $orderItem)

{

	//if($orderItem->plan != null)

	echo $orderItem->plan->planName;

}



In fact I want to list all orderItem. Any suggestions?

Can you please explain what $order is? post some code

OK. Order is just an active record model. The DB schema is as follows.

Order:


orderId

orderDate

clientId

remarks

status

terminateDate

OrderItem:


orderItemId

orderId

productId

type

quantity

status

Plan:


planId

planName

monthlyPrice

status

VAS(value add service):


vasId

vasName

price

status