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?