0

监护人型号:(关系)

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

请指出我如何选择那些在功能中给定 id 的学生的正确方法。我是 yii 的新手。谢谢。

4

1 回答 1

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以确保它是有效值)

于 2013-10-28T09:12:20.473 回答