以下のコードを参照してください。何らかの理由でグループ部分が機能していないため、生成されたクエリにも含まれていません。このため、私たちはfind(なぜ最初に関数でグループを使用したかったのか)では2倍の結果を得ています。CakePHPがグループを検索したのは、生成されたクエリに含まれていません
$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
はありません。 Product.id
でDISTINCT
を試してみましたが、これも機能しません。クエリを実行して、自分でGROUP BY
の部分を実行すると、結果が得られます。group
の部分を'group' => array('Product.id')
のような配列に入れてみましたが、そこには運がありません。
なぜ私たちの検索でgroup
の部分が無視されますか?それはどうやって無視できないのでしょうか?
[カスタムクエリ](https://book.cakephp.org/2.0/en/core-libraries/components/pagination.html#custom-query-pagination)のコード例を見ると、Groupパラメータは次のようになります。配列として供給されます。 – Sevvlor
私たちはすでに配列の中に入れようとしましたが、そこには運がありません。私は私の記事でそれを編集することを明確にすべきだった。 – Femke