关系型AR 怎么获取关联的表的字段值 [ 新手入门 ]
比如在goods模型里面 关联了offer表
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('offer'=>array(self::HAS_MANY,'Offer','goods_id'),);
}
在控制器里面这样写
public function actionTest()
{
echo "i am test";
$goods = Goods::model()->with('offer')->find(array(
'select'=>'goods_name,goods_sn',
'condition'=>'t.goods_id=:goodsId',
'params'=>array(':goodsId'=>62),
));
}
$goods->attributes; 获取的还是goods表的字段值 怎么获取offer的字段值呢?
共 15 条回复
-
有的阿,offer_id 是offer表里面的。
offer 模型如下/** * This is the model class for table "{{offer}}". * * The followings are the available columns in table '{{offer}}': * @property string $offer_id * @property integer $goods_id * @property string $click_count * @property integer $merchant_id * @property string $market_price * @property string $shop_price * @property integer $is_promote * @property string $promote_price * @property string $promote_start * @property string $promote_end * @property integer $region_id * @property integer $is_real * @property integer $is_on_sale * @property string $add_time * @property integer $is_delete * @property string $last_update * @property string $goods_sn * @property string $extension_code * @property string $extension_url * @property integer $before_goods_id */
-
还是一样的效果
Trying to get property of non-object/home/wuyangzhi/projects/myproject_test/backend/protected/controllers/SiteController.php(125)
113 Yii::app()->session->destroy('user'); 114 $this->redirect('index.php?r=site/login'); 115 } 116 117 public function actionTest() 118 { 119 120 $goods = Goods::model()->with('offer')->find(array( 121 'condition'=>'t.goods_id=:goodsId', 122 'params'=>array(':goodsId'=>62), 123 )); 124 125 echo $goods->offer->offer_id; 126 127 } 128 }
-
产生的sql是这样的
Querying SQL: SELECT `t`.`goods_id` AS `t0_c0`, `t`.`cat_id` AS `t0_c1`, `t`.`goods_sn` AS `t0_c2`, `t`.`goods_name` AS `t0_c3`, `t`.`click_count` AS `t0_c4`, `t`.`brand_id` AS `t0_c5`, `t`.`provider_name` AS `t0_c6`, `t`.`goods_number` AS `t0_c7`, `t`.`goods_weight` AS `t0_c8`, `t`.`market_price` AS `t0_c9`, `t`.`shop_price` AS `t0_c10`, `t`.`fitting_price` AS `t0_c11`, `t`.`promote_price` AS `t0_c12`, `t`.`promote_start` AS `t0_c13`, `t`.`promote_end` AS `t0_c14`, `t`.`warn_number` AS `t0_c15`, `t`.`keywords` AS `t0_c16`, `t`.`goods_brief` AS `t0_c17`, `t`.`goods_desc` AS `t0_c18`, `t`.`goods_thumb` AS `t0_c19`, `t`.`goods_img` AS `t0_c20`, `t`.`original_img` AS `t0_c21`, `t`.`is_real` AS `t0_c22`, `t`.`extension_code` AS `t0_c23`, `t`.`is_on_sale` AS `t0_c24`, `t`.`is_alone_sale` AS `t0_c25`, `t`.`is_linked` AS `t0_c26`, `t`.`is_basic` AS `t0_c27`, `t`.`is_gift` AS `t0_c28`, `t`.`can_handsel` AS `t0_c29`, `t`.`integral` AS `t0_c30`, `t`.`add_time` AS `t0_c31`, `t`.`sort_order` AS `t0_c32`, `t`.`is_delete` AS `t0_c33`, `t`.`is_best` AS `t0_c34`, `t`.`is_new` AS `t0_c35`, `t`.`is_hot` AS `t0_c36`, `t`.`is_promote` AS `t0_c37`, `t`.`bonus_type_id` AS `t0_c38`, `t`.`last_update` AS `t0_c39`, `t`.`goods_type` AS `t0_c40`, `t`.`seller_note` AS `t0_c41`, `t`.`cycle_img` AS `t0_c42`, `t`.`extension_url` AS `t0_c43`, `offer`.`offer_id` AS `t1_c0`, `offer`.`goods_id` AS `t1_c1`, `offer`.`click_count` AS `t1_c2`, `offer`.`merchant_id` AS `t1_c3`, `offer`.`market_price` AS `t1_c4`, `offer`.`shop_price` AS `t1_c5`, `offer`.`is_promote` AS `t1_c6`, `offer`.`promote_price` AS `t1_c7`, `offer`.`promote_start` AS `t1_c8`, `offer`.`promote_end` AS `t1_c9`, `offer`.`region_id` AS `t1_c10`, `offer`.`is_real` AS `t1_c11`, `offer`.`is_on_sale` AS `t1_c12`, `offer`.`add_time` AS `t1_c13`, `offer`.`is_delete` AS `t1_c14`, `offer`.`last_update` AS `t1_c15`, `offer`.`goods_sn` AS `t1_c16`, `offer`.`extension_code` AS `t1_c17`, `offer`.`extension_url` AS `t1_c18`, `offer`.`before_goods_id` AS `t1_c19` FROM `hfh_goods` `t` LEFT OUTER JOIN `hfh_offer` `offer` ON (`offer`.`goods_id`=`t`.`goods_id`) WHERE (t.goods_id=:goodsId)
w454638 安徽
注册时间:2011-03-23
最后登录:2015-02-25
在线时长:0小时19分
最后登录:2015-02-25
在线时长:0小时19分
- 粉丝0
- 金钱180
- 威望0
- 积分180