w454638 2011-03-24 11:51:48 5553次浏览 15条回复 0 0 0

比如在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的字段值呢?

  • 回复于 2011-03-24 11:52 举报
    $goods->offer->offer
    

    表的字段;

  • 回复于 2011-03-24 11:54 举报

    不行阿,提示Trying to get property of non-object 错误

  • 回复于 2011-03-24 11:55 举报

    你怎么写的?

  • 回复于 2011-03-24 11:56 举报

    这样写的

    public function actionTest()
    {
      $goods = Goods::model()->with('offer')->find(array(
        'select'=>'goods_name,goods_sn',
        'condition'=>'t.goods_id=:goodsId',
        'params'=>array(':goodsId'=>62),
      ));	
      echo $goods->offer->offer_id;
    }
    
  • 回复于 2011-03-24 12:00 举报

    你的offer表有offer_id吗?
    这具offer_id好像是goods表里的吧

    觉得很赞
  • 回复于 2011-03-24 12:02 举报

    有的阿,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
     */
    
  • 回复于 2011-03-24 12:03 举报
    $goods = Goods::model()->with('offer')->find(array(
      'select'=>'goods_name,goods_sn',   //把select去掉试试
      'condition'=>'t.goods_id=:goodsId',
      'params'=>array(':goodsId'=>62),
    ));
    
  • 回复于 2011-03-24 12:05 举报

    还是一样的效果
    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 }
    
  • 回复于 2011-03-24 12:11 举报

    你好好看看有没有重名的字段,offer这个关联,最好改一下名,别和Offer这个模型类冲突了。

  • 回复于 2011-03-24 12:11 举报

    产生的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)
    
  • 回复于 2011-03-24 12:15 举报

    但是

    print_r($goods->offer);
    

    是正常的

  • 回复于 2011-03-24 12:17 举报
    $goods = Goods::model()->find(array(
      'condition'=>'t.goods_id=:goodsId',
      'params'=>array(':goodsId'=>62),
    ));
    

    把with去掉,把select去掉。

    $goods->offer->offer_id;
    

    再看一下有没有值

  • 回复于 2011-03-24 12:24 举报

    可以了 要这样

    $goods = Goods::model()->find(array(
      'condition'=>'t.goods_id=:goodsId',
      'params'=>array(':goodsId'=>62),
    ));
    $offer =  $goods->offer;
    foreach($offer as $item)
    {
      echo $item->getAttribute('offer_id');
    }
    

    感谢舰长阿。。

  • 回复于 2011-03-24 12:25 举报

    呵呵,不客气!

  • 回复于 2011-03-25 14:40 举报

    舰长太给力了!~厉害,佩服

您需要登录后才可以回复。登录 | 立即注册