[英]unable to fetch records from multiple tables using join
我正在使用Yii框架。 我想知道如何從做過研究的多個表中獲取記錄,但找不到任何有用的鏈接,我為此使用了以下代碼,請讓我知道我在哪里缺少
我的模型Task.php
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'prj_user' => array(self::BELONGS_TO, 'User', 'id'),
);
}
模型User.php
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
array('task', self::HAS_MANY, 'Task','project_id')
);
}
這是我的主要控制者
$criteria = new CDbCriteria;
$criteria->compare('t.id', 1);
$criteria->with = array( 'prj_user' => array('select' => 'username,title,roles', 'joinType'=>'inner join'));
$rows = Task::model()->findAll( $criteria );
但我仍然只從任務表中獲取列,但我需要從用戶表中獲取更多三列,請幫助我
讓Yii擔心加入您的桌子。 您的關系看起來不錯,因此您應該可以直接訪問它們
例如,這返回什么?
foreach ($rows as $task)
{
if ( isset($task->prj_user) )
echo $task->prj_user->username . "<br>";
}
或這個?
this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>new CActiveDataProvider('Task'),
'columns'=>array(
'id',
'prj_user.username',
'prj_user.title',
'prj_user.roles',
)
));
-> with()用於急切加載,因此此時您可能不需要它。 實際上,除非我完全誤解了您,否則您可以一起刪除所有條件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.