yii多表關聯查詢後的資料怎麼讀出?

來源:互聯網
上載者:User
我在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欄位,其他的按照欄位拿就行。

  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.