CakePHP明显挑选额外的列

时间:2014-04-07 16:13:01

标签: cakephp distinct

我在CakePHP的一个项目上工作但遇到了一个奇怪的问题。我正在写一个查询:

    $brands = $this->Product->find('all', array(
            'fields'=> array('DISTINCT Product.brand as brand'),
            'order'=>'Product.brand ASC',
            'conditions'=> array('Product.subcategory_id'=>$subcategory_id)
    ));

它正在选择Product.id和Product.brand,我不想要它。

它生成的查询是:

SELECT DISTINCT `Product`.`brand`, `Product`.`id` FROM `ecom`.`products` AS `Product` LEFT JOIN `ecom`.`subcategories` AS `SubCategory` ON (`Product`.`subcategory_id` = `SubCategory`.`id`) LEFT JOIN `ecom`.`categories` AS `Category` ON (`Product`.`category_id` = `Category`.`id`) WHERE `Product`.`subcategory_id` = 13 ORDER BY `Product`.`brand` ASC

如何从select?中跳过Product.id?

由于

2 个答案:

答案 0 :(得分:0)

添加群组解决了我的问题:

    $brands = $this->Product->find('all', array(
            'fields'=> array('DISTINCT Product.brand as brand'),
            'order'=>'Product.brand ASC',
            'conditions'=> array('Product.subcategory_id'=>$subcategory_id),
            'group' => 'brand'
    ));

答案 1 :(得分:0)

我认为这是因为cake需要id来创建连接

尝试

$brands = $this->Product->find('all', array(
        'fields'=> array('DISTINCT Product.brand as brand'),
        'order'=>'Product.brand ASC',
        'conditions'=> array('Product.subcategory_id'=> $subcategory_id),
        'recursive' => -1
));