CakePHP查找分组不在生成的查询中

时间:2017-03-22 13:12:44

标签: mysql cakephp group-by find cakephp-2.0

我们有一个相当全面的查找,请参阅下面的代码,并按部分分组。由于某种原因,组部分不起作用,甚至在生成的查询中也是如此。因此,我们在查找中得到了双重结果(为什么我们想要首先使用按功能分组)。

$this->Product->Behaviors->load('Containable');
$this->paginate = array(
    'contain' => array(
        'Reduction' => array(
            'fields' => array(
                'Reduction.id', 'Reduction.startdate', 'Reduction.enddate', 'Reduction.price'
            ),
            'conditions' => array(
                'Reduction.startdate <' => date('Y-m-d H:00:00'),
                'Reduction.enddate >' => date('Y-m-d H:00:00'),
            ),
        ),
        'Mediafile',
        'Deliverytime' => array(
            'fields' => array(
                'Deliverytime.id', 'Deliverytime.name'
            )
        )
    ),
    'fields' => array(
        'Product.id', 'Product.name', 'Product.slug', 'Product.price', 'Product.content_short', 'Product.metadescription', 'Product.name_short',
        'Category.id', 'Category.name', 'Category.slug',
    ),
    'conditions' => array(
        'Product.status' => 'active',
        'Product.visibility' => 1,
        'Product.hidden_on_site' => 0,
        'OR' => array(
            array(
                'AND' => array(
                    array('Product.use_stock_count' => 0)
                )
            ),
            array(
                'AND' => array(
                    array('Product.use_stock_count' => 1),
                    array('Product.stock_count >=' => 1)
                )
            ),
        ),
    ),
    'joins' => array(
        array(
            'table' => 'products_categories',
            'alias' => 'ProductsCategory',
            'type' => 'INNER',
            'conditions' => array(
                'ProductsCategory.category_id' => $cat_ids,
                'ProductsCategory.product_id = Product.id',
            )
        ),
        array(
            'table' => 'categories',
            'alias' => 'Category',
            'type' => 'INNER',
            'conditions' => array(
                'Category.id = ProductsCategory.category_id',
            )
        ),
    ),
    'order' => 'Product.price ASC',
    'group' => 'Product.id',
    'limit' => $limit,
    'recursive' => -1,
    'page' => $page_number,
);
$products = $this->paginate('Product');

这是生成的查询:

SELECT `Product`.`id`, `Product`.`name`, `Product`.`slug`, `Product`.`price`, `Product`.`content_short`, `Product`.`metadescription`, `Product`.`name_short`, `Category`.`id`, `Category`.`name`, `Category`.`slug`, `Deliverytime`.`id`, `Deliverytime`.`name`, `Product`.`id` 
FROM `products` AS `Product` 
LEFT JOIN `deliverytimes` AS `Deliverytime` ON (`Product`.`deliverytime_id` = `Deliverytime`.`id`) 
INNER JOIN `products_categories` AS `ProductsCategory` ON (`ProductsCategory`.`category_id` IN ('15', '306', '308', '309', '26', '167', '248', '185', '115', '116', '23', '258', '201', '22', '16', '17', '25', '19', '18', '114', '27', '127', '158', '136') AND `ProductsCategory`.`product_id` = `Product`.`id`) 
INNER JOIN `categories` AS `Category` ON (`Category`.`id` = `ProductsCategory`.`category_id`)  
WHERE `Product`.`status` = 'active' AND `Product`.`visibility` = '1' AND `Product`.`hidden_on_site` = '0' AND ((`Product`.`use_stock_count` = '0') OR (((`Product`.`use_stock_count` = '1') AND (`Product`.`stock_count` >= 1)))) 
ORDER BY `Product`.`price` ASC  
LIMIT 18

如您所见,没有GROUP BY。我们还尝试在DISTINCT上使用Product.id,但这也不起作用。如果您自己运行查询并在GROUP BY部分,我们会得到我们想要的结果。我们也尝试将group部分放在像'group' => array('Product.id')这样的数组中,但没有运气。

那么为什么我们发现忽略了group部分?我们怎样才能做到这一点,不被忽视?

1 个答案:

答案 0 :(得分:0)

原来这是我们自己的错。我们团队中的某个人制作了一个自定义分页功能,但没有传递组变量。那时我还没有意识到这一点。当我在不同的项目中使用他的功能时,我只是有一个“我现在得到它”的时刻。