Chủ Nhật, 26 tháng 8, 2012

yii ~ model relation get value example

product_internet_recommend

裡面有個productID是外來建為Product的id


models/ProductInternetRecommend.php

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(
'product' => array(self::BELONGS_TO, 'Product', 'productID'),
);
}


in controller

$productInternet = ProductInternetRecommend::model() ->with('product')-> findByPk(1);


in view

echo 'asd'.$productInternet->product->price;



reference
http://stackoverflow.com/questions/9857209/yii-relations-error-trying-to-get-property-of-non-object

http://www.yiiframework.com/doc/guide/1.1/en/database.arr

Không có nhận xét nào:

Đăng nhận xét