CGridView에서 관련 모델별로 검색 및 정렬에 대한 포괄적 인 기사가 있습니다. http://www.yiiframework.com/wiki/281/searching-and-sorting-by-related-model-in-cgridview/ 여러 번이 요리법을 성공적으로 구현했습니다. .관계가 동일한 테이블의 키인 경우 Yii의 관련 모델을 기준으로 CGridView로 검색 및 정렬
그러나 이제는 동일한 테이블에있는 관계로 검색하고 정렬하려고합니다 (parent_id가 정의 됨). 참조 '부모'와 관련하여 아래 :
public function relations()
{
return array(
'parent' => array(self::BELONGS_TO, 'Category', 'parent_id'),
'children' => array(self::HAS_MANY, 'Category', 'parent_id'),
'childCount' => array(self::STAT, 'Category', 'parent_id'),
'author0' => array(self::BELONGS_TO, 'User', 'author'),
'contents' => array(self::MANY_MANY, 'Content', 'content_category(content_id, category_id)'),
'crsContents' => array(self::MANY_MANY, 'ContentCrs', 'content_category(content_id, category_id)'),
);
}
public function defaultScope()
{
return array(
'alias'=>'cat',
'order'=>"cat.name ASC",
);
}
위키에 지정된대로 내가 방법을 구현할 때, 나는 다음과 같은 오류 얻을 :
CDbCommand failed to execute the SQL statement: SQLSTATE[42000]: Syntax error or access violation: 1066 Not unique table/alias: 'cat'. The SQL statement executed was: SELECT COUNT(DISTINCT `cat`.`id`) FROM `category` `cat` LEFT OUTER JOIN `category` `cat` ON (`cat`.`parent_id`=`cat`.`id`) WHERE (cat.parent_id <> 257)
가 어떻게이 LEFT OUTER 고유 사용 가입하도록 할 수 있습니다 테이블 별칭은 parent
로의 관계에 대해 내가 제대로 내 CDbCriteria
search()
에서 편집을 정의 할 수 있도록 :
@ tereško가 요청한대로 여기 내 search() 함수가 있습니다. 나는 그것을 정의하지 않았을 때 테이블 별칭 parent
이 지정 되었기 때문에 결함이 있다는 것을 알고있다 ... 나는 단지 어떻게 알지 못한다! 당신이 당신의 검색() 코드를 붙여 넣을 수 있습니다
public function search()
{
$criteria=new CDbCriteria;
$criteria->with = array('parent');
$criteria->compare('id',$this->id,true);
$criteria->compare('author',$this->author,true);
$criteria->compare('name',$this->name,true);
$criteria->compare('description',$this->description,true);
$criteria->compare('parent_id',$this->parent_id,true);
$criteria->compare('parent.name', $this->parent_search, true);
$criteria->compare('type',$this->type,true);
$criteria->compare('slug',$this->slug,true);
$criteria->compare('created',$this->created,true);
$criteria->compare('updated',$this->updated,true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
'sort'=>array(
'attributes'=>array(
'parent_search'=>array(
'asc'=>'parent.name',
'desc'=>'parent.name DESC',
),
'*',
),
),
)
);
}
다음과 같이 부모 관계에 대한 별칭을 줄 수 있습니까? – GBD
@GBD : 나는 그것을 추가했다. 어떤 아이디어? – Adam
'$ criteria-> with array ('parent'=> array ('alias'=> 'p')); ' – GBD