0

i'm working on a project in CakePHP but stuck at a strange problem. I'm writing a query:

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

It is picking Product.id along with Product.brand which I do not want.

The query it generates is:

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

How can I skip Product.id from select?

Thanks

Ashutosh
  • 4,371
  • 10
  • 59
  • 105

2 Answers2

0

Adding group solved my issue:

    $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'
    ));
Ashutosh
  • 4,371
  • 10
  • 59
  • 105
0

I think it's because cake needs the id to create the joins

try

$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
));
arilia
  • 9,373
  • 2
  • 20
  • 44