![](/img/trans.png)
[英]Searching and sorting by related model in Yii with CGridView when Relation is a key in the same table
[英]Searching and sorting by related model in CGridView Yii
閱讀此文件后,我想使我的CGridView列可搜索,問題是當我使用一個cdbCriteria->with
條件時,它可以正常工作。 但是當我添加多個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'),
); }
當我使用@Nisic和@ Noam148時
$criteria->with=array('teacherGradeSection','course','teacher ')
我收到以下錯誤
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`)
嘗試定義兩個可以解決沖突的關系
'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' );
您的第二個with
實際上將替換您的第一個with
因此您不會急於加載該課程,因此當您稍后在compare
引用它時,您會得到錯誤提示。
您只需要准備課程和老師的一個即可。 我不確定在沒有數據庫模式的情況下如何編寫它,但是您可以盲目嘗試:
$criteria->with = array( 'teacherGradeSection','course', 'teacher' );
或者只是向我們解釋關系
您可以一次使用$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 );
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.