数据提供者关系未获取所需数据

时间:2013-10-28 08:59:12

标签: php yii

守护者模型:(关系)

'studentsGuardians' => array(self::HAS_MANY, 'StudentsGuardian', 'guardian_id'),

学生模特关系:

'studentsGuardians' => array(self::HAS_MANY, 'StudentsGuardian', 'student_id'),

学生卫士模特关系:

'guardian' => array(self::BELONGS_TO, 'Guardian', 'guardian_id'),
                        'student' => array(self::BELONGS_TO, 'Student', 'student_id'),

现在在控制器中我想选择那些guardian_id = id但我的代码选择所有记录而不进行过滤的学生。我的代码是

public function actionAssignGuardian($id)
        {

            $dataProvider = new CActiveDataProvider('Student',
                                array(
                                    'criteria' => array(
                                        'with'=>array('studentsGuardians',
                                            array('criteria'=>
                                            array('with'=>array('guardian','condition'=>' guardian_id=:id',
                                                'params'=>array('id'=>$id))))),

                                    ),

                                    ));



            $this->renderPartial('Pages/_assignGuardian', array(
                'dataProvider' => $dataProvider,
                'id'=>$id,
                    ));
        }

请指出我正确的方法,我怎样才能选择那些guardian_id =在功能上给予id的学生。我是yii的新手。 谢谢。

1 个答案:

答案 0 :(得分:0)

将此行添加到Student模型中(在上一个关系行studentsGuardians下面)

'guardians' => array(self::HAS_MANY, 'Guardian', array('guardian_id'=>'guardian_id'),'through'=>'studentsGuardians'),

然后你可以像这样进行查询

$dataProvider = new CActiveDataProvider('Student',
            array(
                'criteria' => array(
                    'with'=>array(
                        'guardians' =>array(
                            'condition'=>' guardian_id=:id',
                            'params'=>array('id'=>$id)
                        )))    
            ));

(您还应该转储$id以确保它是有效值)