无法使用join从多个表中获取记录

时间:2013-06-13 02:20:50

标签: php yii

我正在使用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 );

但我仍然只从任务表获取列,但我需要更多来自用户表的三列,请帮助我

1 个答案:

答案 0 :(得分:0)

让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',
    )
));

- &gt; with()用于预先加载,因此此时您可能不需要它。事实上,除非我完全误读你,否则你可以一起删除你的标准。

相关问题