有两个表,attendee
和group
。两者都有一个gender
字段。每位与会者被分配到1个组。我已经成功显示了CGridView
与会者人数,但是当我添加代码来过滤性别时,我收到以下错误。要计算组中与会者的数量,我手动执行加入;但是联接使得“性别”字段模糊不清。有没有办法在过滤器中指定它应该使用't'或基表?
当我加载视图时,它正常运行;当我点击对gender
进行排序时,它会给我这样的信息:
> Error 500: <h1>CDbException</h1>
> <p>CDbCommand failed to execute the SQL statement: SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'gender'
> in where clause is ambiguous. The SQL statement executed was: SELECT
> COUNT(*) FROM (SELECT t.*, IFNULL( count(tbl_attendee.id), 0) as
> attendeeCount FROM `tbl_group` `t` LEFT JOIN tbl_attendee ON
> tbl_attendee.group_id = t.id WHERE gender LIKE :ycp0 GROUP BY t.id) sq
> (/Applications/MAMP/htdocs/yiiroot/framework/db/CDbCommand.php:516)</p><pre>#0
> /Applications/MAMP/htdocs/yiiroot/framework/db/CDbCommand.php(411):
> CDbCommand->queryInternal('fetchColumn', 0, Array)
$criteria=new CDbCriteria;
$criteria->compare('name',$this->name,true);
$criteria->compare('gender',$this->gender,true);
$criteria->compare('agegroup',$this->agegroup,true);
$criteria->select = 't.*, IFNULL( count(tbl_attendee.id), 0) as attendeeCount';
$criteria->join = 'LEFT JOIN tbl_attendee ON tbl_attendee.group_id = t.id';
$criteria->group = 't.id';
$sort = new CSort();
$sort->attributes = array(
'attendeeCount'=>array(
'asc'=>'attendeeCount ASC',
'desc'=>'attendeeCount DESC',
),
'*', // add all of the other columns as sortable
);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
'sort'=>$sort,
));
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'group-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'name',
array(
'name'=>'gender',
'value'=>'Group::$genderLabels[$data->gender]',
'filter'=>Group::$genderLabels, // <-- This is the filter that breaks.
// $genderLabels = array('Female','Male);
),
答案 0 :(得分:5)
啊!我知道了。
在模型代码中更改了这一行:
$criteria->compare('gender',$this->gender,true);
到
$criteria->compare('t.gender',$this->gender,true);