yii多表关联查询后的数据怎么读出?

WBOY
发布: 2016-06-06 20:24:19
原创
1119 人浏览过

我在Surl.php中定义了方法

<code>    public function relations(){ // 这里我进行了关联。主表surl的duid关联了user表的uid(主键) 
        return array(  
            'user'=>array(self::BELONGS_TO, 'User', 'duid'),  
        );
    } 
    /* 获取短址 */
    public function getList(){
        $result = Surl::model()->findByPk(50);
        $criteria=new CDbCriteria; 
        $criteria->with = array('user');
        $result = Surl::model()->findAll($criteria);
        var_dump($result); // 打印$result
    }</code>
登录后复制
登录后复制

这个是我的打印结果

<code>array(391) {
  [0] => object(Surl)#64 (11) {
    ["_new":"CActiveRecord":private] => bool(false)
    ["_attributes":"CActiveRecord":private] => array(11) {
      ["id"] => string(2) "21"
      ["duid"] => string(1) "3"
    }
    ["_related":"CActiveRecord":private] => array(1) {
      ["user"] => object(User)#65 (11) {
        ["_new":"CActiveRecord":private] => bool(false)
        ["_attributes":"CActiveRecord":private] => array(10) {
          ["uid"] => string(10) "3"  
          ["name"] => string(10) "渠道商B"
        }
        ["_related":"CActiveRecord":private] => array(0) {
        }
        ["_c":"CActiveRecord":private] => NULL
        ["_pk":"CActiveRecord":private] => string(1) "3"
        ["_alias":"CActiveRecord":private] => string(1) "t"
        ["_errors":"CModel":private] => array(0) {
        }
        ["_validators":"CModel":private] => NULL
        ["_scenario":"CModel":private] => string(6) "update"
        ["_e":"CComponent":private] => NULL
        ["_m":"CComponent":private] => NULL
      }
    }
    ["_c":"CActiveRecord":private] => NULL
    ["_pk":"CActiveRecord":private] => string(2) "21"
    ["_alias":"CActiveRecord":private] => string(1) "t"
    ["_errors":"CModel":private] => array(0) {
    }
    ["_validators":"CModel":private] => NULL
    ["_scenario":"CModel":private] => string(6) "update"
    ["_e":"CComponent":private] => NULL
    ["_m":"CComponent":private] => NULL
  }</code>
登录后复制
登录后复制

我想问问各位啊 我该如何将user表的数据读出来啊!
foreach ($result as $key => $value) {}报错啊!

回复内容:

我在Surl.php中定义了方法

<code>    public function relations(){ // 这里我进行了关联。主表surl的duid关联了user表的uid(主键) 
        return array(  
            'user'=>array(self::BELONGS_TO, 'User', 'duid'),  
        );
    } 
    /* 获取短址 */
    public function getList(){
        $result = Surl::model()->findByPk(50);
        $criteria=new CDbCriteria; 
        $criteria->with = array('user');
        $result = Surl::model()->findAll($criteria);
        var_dump($result); // 打印$result
    }</code>
登录后复制
登录后复制

这个是我的打印结果

<code>array(391) {
  [0] => object(Surl)#64 (11) {
    ["_new":"CActiveRecord":private] => bool(false)
    ["_attributes":"CActiveRecord":private] => array(11) {
      ["id"] => string(2) "21"
      ["duid"] => string(1) "3"
    }
    ["_related":"CActiveRecord":private] => array(1) {
      ["user"] => object(User)#65 (11) {
        ["_new":"CActiveRecord":private] => bool(false)
        ["_attributes":"CActiveRecord":private] => array(10) {
          ["uid"] => string(10) "3"  
          ["name"] => string(10) "渠道商B"
        }
        ["_related":"CActiveRecord":private] => array(0) {
        }
        ["_c":"CActiveRecord":private] => NULL
        ["_pk":"CActiveRecord":private] => string(1) "3"
        ["_alias":"CActiveRecord":private] => string(1) "t"
        ["_errors":"CModel":private] => array(0) {
        }
        ["_validators":"CModel":private] => NULL
        ["_scenario":"CModel":private] => string(6) "update"
        ["_e":"CComponent":private] => NULL
        ["_m":"CComponent":private] => NULL
      }
    }
    ["_c":"CActiveRecord":private] => NULL
    ["_pk":"CActiveRecord":private] => string(2) "21"
    ["_alias":"CActiveRecord":private] => string(1) "t"
    ["_errors":"CModel":private] => array(0) {
    }
    ["_validators":"CModel":private] => NULL
    ["_scenario":"CModel":private] => string(6) "update"
    ["_e":"CComponent":private] => NULL
    ["_m":"CComponent":private] => NULL
  }</code>
登录后复制
登录后复制

我想问问各位啊 我该如何将user表的数据读出来啊!
foreach ($result as $key => $value) {}报错啊!

$result[0]->user就得到了user下面的所有信息,再继续调用就可以了。
$result[0]->user->id。拿到的就是user表里面的id字段,其他的按照字段拿就行。

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!