当前位置:Gxlcms > PHP教程 > yii多表关联查询后的数据怎么读出?

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

时间:2021-07-01 10:21:17 帮助过:6人阅读

我在Surl.php中定义了方法

    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
    }

这个是我的打印结果

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
  }

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

回复内容:

我在Surl.php中定义了方法

    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
    }

这个是我的打印结果

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
  }

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

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

人气教程排行