我在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欄位,其他的按照欄位拿就行。