我正在编写一个 CakePHP 1.2 应用程序。我有一个我希望用户能够在不同字段上过滤的人员列表。对于每个可过滤字段,我都有一个下拉列表。选择过滤器组合,点击过滤器,页面只显示匹配的记录。
在 people_controller 中,我有这段代码:
$first_names = $this->Person->find('list', array(
'fields'=>'first_name',
'order'=>'Person.first_name ASC',
'conditions'=> array('Person.status'=>'1')
));
$this->set('first_names', $first_names);
(状态 = 1,因为我正在使用软删除。)
这将创建所有 first_names 的有序列表。但是那里有重复项。
在 Cookbook 中四处挖掘,我找到了一个使用 DISTINCT 关键字的示例,并修改了我的代码以使用它。
$first_names = $this->Person->find('list', array(
'fields'=>'DISTINCT first_name',
'order'=>'Person.first_name ASC',
'conditions'=> array('Person.status'=>'1')
));
这给了我这样的 SQL 错误:
Query: SELECT `Person`.`id`, DISTINCT `Person`.` first_name` FROM `people` AS `Person` WHERE `Person`.`status` = 1 ORDER BY `Person`.`first_name` ASC
问题很明显。该框架正在将 Person.id 添加到查询中。我怀疑这来自使用“列表”。
单击过滤器按钮时,我将使用选定的过滤器创建一条 SQL 语句。我不需要 is 字段,但无法摆脱它。
谢谢你,弗兰克·卢克