Yii CActiveDataProvider MANY :: MANY

时间:2013-06-18 19:53:34

标签: php yii

我有两个模型:项目和用户连接(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(
            "projects"=>array(self::MANY_MANY, 'Project','projects_users(user_id, project_id)'),
    );
}

我想向CActiveDataProvider中显示与项目连接的所有用户。我该怎么办?

2 个答案:

答案 0 :(得分:1)

我找到了解决方案:

$criteria=new CDbCriteria;

foreach($model->users as $cur) {
    $criteria->addCondition("ID != ".$cur->ID);
    }

$users=User::model()->findAll($criteria);

$dataProvider2=new CActiveDataProvider('User');
$dataProvider2->data = $users;

答案 1 :(得分:0)

试试这个:

$users = User::model()->with('projects')->findAll(array(
    'together' => true,
    'condition' => 'projects.id IS NULL',
));