Home > Backend Development > PHP Tutorial > yii多表关联查询后的数据怎么读出?

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

WBOY
Release: 2016-06-06 20:24:19
Original
1137 people have browsed it

我在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>
Copy after login
Copy after login

这个是我的打印结果

<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>
Copy after login
Copy after login

我想问问各位啊 我该如何将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>
Copy after login
Copy after login

这个是我的打印结果

<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>
Copy after login
Copy after login

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

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

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template