2015-10-20 16:39:56 4427次浏览 2条回答 0 悬赏 100 金钱

先说明下t表的user_id字段跟s表的id字段是关联的(t表的主键id字段,s表同样)

        $criteria=new CDbCriteria;
        $criteria->select = 's.* ';
        $criteria->condition='t.user_id=:user_id and t.is_deleted=:is_deleted';
        $criteria->params=array(':user_id'=>$user_id,':is_deleted'=>self::IS_DELETED_NO);
        $criteria->order = 't.id desc';
        $criteria->join = "LEFT JOIN user AS s ON t.user_id=s.id";
        $this->findAll($criteria);

打印出的SQL语句为:

SELECT s.* FROM `message` `t` LEFT JOIN user AS s ON s.id=t.user_id WHERE t.user_id=5 and t.is_deleted=0 ORDER BY t.id desc 

把SQL拿到数据库里可以运行,而且也能得到s表所有字段数据,
但是我var_dump()这个查询出来的数据,发现只有两个字段,其余的字段都查不到

 private '_attributes' (CActiveRecord) => 
        array (size=2)
          'id' => string '636' (length=3)
          'status' => string '1' (length=1)

这里我把s.*改为t.*,s.username

        $criteria=new CDbCriteria;
        $criteria->select = 't.*,s.username';
        $criteria->condition='t.user_id=:user_id and t.is_deleted=:is_deleted';
        $criteria->params=array(':user_id'=>$user_id,':is_deleted'=>self::IS_DELETED_NO);
        $criteria->order = 't.id desc';
        $criteria->join = "LEFT JOIN user AS s ON t.user_id=s.id";
        $this->findAll($criteria);

接着var_dump()这个查询出来的数据,发现能查出来s表所有字段,但是t表username,根本就没有出现。

private '_attributes' (CActiveRecord) => 
        array (size=8)
          'id' => string '2099' (length=4)
          'user_id' => string '636' (length=3)
          'title' => string 'You've received a new incentive.' (length=32)
          'type' => string 'notification' (length=12)
          'content' => string '{"rewardsId":"161"}' (length=19)
          'status' => string '1' (length=1)
          'createdtime' => string '1445308021' (length=10)
          'is_deleted' => string '0' (length=1)

同样拿打印出来的SQL语句放到数据库里面直接查询,可以查到s表所有字段+t表username字段
求大神解答??

  • 回答于 2015-10-27 14:54 举报

    $command = Admin::find()->select('tbl_user.,tbl_admin.')->join('LEFT JOIN','tbl_user','tbl_user.admin_id = tbl_admin.id')->asArray()->all();

  • 回答于 2016-04-12 09:39 举报

    怎么打印sql语句的!?

您需要登录后才可以回答。登录 | 立即注册
某渣
助理

某渣

注册时间:2015-05-02
最后登录:2017-10-28
在线时长:4小时44分
  • 粉丝2
  • 金钱159
  • 威望0
  • 积分199

热门问题