我希望在阅读this后可以搜索我的CGridView列,问题是当我使用一个cdbCriteria->with
条件时它工作正常。但是当我添加多个cdbCriteria->with
条件时,它会出错。更具体地说,我在这里粘贴我的代码。
$criteria=new CDbCriteria;
$criteria->with = array( 'teacherGradeSection','course' );
$criteria->with = array( 'teacherGradeSection','teacher' );
$criteria->together = true;
$criteria->compare('course.name', $this->course_search, true );
$criteria->compare('teacher.firstname', $this->teacher_search, true );
当我只使用一个条件时它工作正常,但是当使用多个条件时会出错。
的错误
SQLSTATE[42S22]: Column not found: 1054 Unknown column
编辑1: 这些是可用的关系
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(
'submissions' => array(self::HAS_MANY, 'Submission', 'task_id'),
'teacherGradeSection' => array(self::BELONGS_TO, 'TeacherGradeSection', 'teacher_grade_section_id'),
'course' => array(self::HAS_MANY, 'Course', array('course_id'=>'id'),'through'=>'teacherGradeSection'),
'teacher'=> array(self::HAS_MANY,'Teacher',array('teacher_teacher_id'=>'teacher_id'),'through'=>'teacherGradeSection'),
); }
当我使用左$criteria->with=array('teacherGradeSection','course','teacher ')
时被@Nisic和@ Noam148所取代
我收到以下错误
CDbCommand failed to execute the SQL statement: SQLSTATE[42000]: Syntax error or access violation: 1066 Not unique table/alias: 'teacherGradeSection'. The SQL statement executed was: SELECT COUNT(DISTINCT `t`.`id`) FROM `task` `t` LEFT OUTER JOIN `teacher_grade_section` `teacherGradeSection` ON (`t`.`teacher_grade_section_id`=`teacherGradeSection`.`id`) LEFT OUTER JOIN `course` `course` ON (`course`.`id`=`teacherGradeSection`.`course_id`) LEFT OUTER JOIN `teacher_grade_section` `teacherGradeSection` ON (`t`.`teacher_grade_section_id`=`teacherGradeSection`.`id`) LEFT OUTER JOIN `teacher` `teacher` ON (`teacher`.`teacher_id`=`teacherGradeSection`.`teacher_teacher_id`)
答案 0 :(得分:1)
尝试定义两个将解决冲突的关系
'teacherGradeSection' => array(self::BELONGS_TO, 'TeacherGradeSection', 'teacher_grade_section_id'),
'teacherGradeSectiontwo' => array(self::BELONGS_TO, 'TeacherGradeSection', 'teacher_grade_section_id'),
并在您的标准中
$criteria->with['course'] = array( 'teacherGradeSection','course' );
$criteria->with['teacher']= array( 'teacherGradeSectiontwo','teacher' );
答案 1 :(得分:0)
您的第二个with
实际上会替换您的第一个with
,因此您不会急于加载课程,所以当您稍后在compare
中引用它时,您会收到错误。
你只需要一个人,课程和老师。我无法确定如何在没有数据库架构的情况下编写它,但你可以盲目尝试:
$criteria->with = array( 'teacherGradeSection','course', 'teacher' );
或者只是向我们解释关系
答案 2 :(得分:0)
您可以使用$criteria->with
属性一次。当您第二次使用$criteria->with
时,覆盖第一次。
请在此处查看如何使用with属性。通过这种方式,您可以为您的CDbCriteria':
添加3种不同的关系$criteria=new CDbCriteria;
$criteria->with = array( 'teacherGradeSection','course','teacher');
$criteria->together = true;
$criteria->compare('course.name', $this->course_search, true );
$criteria->compare('teacher.firstname', $this->teacher_search, true );