【问题标题】:CakePHP how I will fetch category wise products?CakePHP 我将如何获取类别明智的产品?
【发布时间】:2021-11-24 23:13:42
【问题描述】:

我有 3 张桌子

categories, product_categories , products 

这里是categories hasMany product_categoriesproduct_categories hasMany products。我正在尝试获取 类别下的最新 20 种产品

我在下面写了查询

$categories = \Cake\ORM\TableRegistry::getTableLocator()->get( 'Categories' )->find()
    ->contain(
                [
                    'ProductCategories.Products' => function($q){
                            return $q->limit(20);
                    }
                ]
     )

通过这个查询,我得到每个 ProductCategories 的 20 个产品。我只需要按 ProductCategories 分类的 20 种产品。我该如何解决这个问题?

@ndm 评论后我尝试了下面的库https://github.com/icings/partitionable

在 CategoriesTable 表中我写了下面的代码

$this
      ->partitionableBelongsToMany('RecentProducts')
      ->setClassName('Products')
      ->setThrough('ProductCategories')
      ->setLimit(9)
      ->setSort([
          'RecentProducts.created' => 'DESC',
      ])
;

我的查询看起来像

SELECT 
  ProductCategories.id AS RecentProducts_CJoin__id, 
  ProductCategories.name AS RecentProducts_CJoin__name, 
  ProductCategories.slug AS RecentProducts_CJoin__slug, 
  ProductCategories.img AS RecentProducts_CJoin__img, 
  ProductCategories.category_id AS RecentProducts_CJoin__category_id, 
  ProductCategories.sub_category_id AS RecentProducts_CJoin__sub_category_id, 
  ProductCategories.created AS RecentProducts_CJoin__created, 
  ProductCategories.modified AS RecentProducts_CJoin__modified, 
  RecentProducts.id AS RecentProducts__id, 
  RecentProducts.name AS RecentProducts__name, 
  RecentProducts.item_id AS RecentProducts__item_id, 
  RecentProducts.product_category_id AS RecentProducts__product_category_id, 
  RecentProducts.crawl_uniquekey AS RecentProducts__crawl_uniquekey, 
  RecentProducts.website_name AS RecentProducts__website_name, 
  RecentProducts.original_price AS RecentProducts__original_price, 
  RecentProducts.discount_amount AS RecentProducts__discount_amount, 
  RecentProducts.description AS RecentProducts__description, 
  RecentProducts.img_url AS RecentProducts__img_url, 
  RecentProducts.total_review AS RecentProducts__total_review, 
  RecentProducts.review_average AS RecentProducts__review_average, 
  RecentProducts.price AS RecentProducts__price, 
  RecentProducts.percentage AS RecentProducts__percentage, 
  RecentProducts.expiry AS RecentProducts__expiry, 
  RecentProducts.price_after_discount AS RecentProducts__price_after_discount, 
  RecentProducts.price_zero_padded AS RecentProducts__price_zero_padded, 
  RecentProducts.created AS RecentProducts__created, 
  RecentProducts.modified AS RecentProducts__modified 
FROM 
  products RecentProducts 
  INNER JOIN product_categories ProductCategories ON ProductCategories.id = (
    RecentProducts.product_category_id
  ) 
WHERE 
  (
    ProductCategories.category_id in (1, 2) 
    AND ProductCategories.id in (
      SELECT 
        __ranked__RecentProducts.id AS id 
      FROM 
        (
          SELECT 
            ProductCategories.id AS RecentProducts_CJoin__id, 
            ProductCategories.name AS RecentProducts_CJoin__name, 
            ProductCategories.slug AS RecentProducts_CJoin__slug, 
            ProductCategories.img AS RecentProducts_CJoin__img, 
            ProductCategories.category_id AS RecentProducts_CJoin__category_id, 
            ProductCategories.sub_category_id AS RecentProducts_CJoin__sub_category_id, 
            ProductCategories.created AS RecentProducts_CJoin__created, 
            ProductCategories.modified AS RecentProducts_CJoin__modified, 
            ProductCategories.id AS id, 
            (
              ROW_NUMBER() OVER (
                PARTITION BY ProductCategories.category_id 
                ORDER BY 
                  RecentProducts.created DESC
              )
            ) AS __row_number, 
            RecentProducts.id AS RecentProducts__id, 
            RecentProducts.name AS RecentProducts__name, 
            RecentProducts.item_id AS RecentProducts__item_id, 
            RecentProducts.product_category_id AS RecentProducts__product_category_id, 
            RecentProducts.crawl_uniquekey AS RecentProducts__crawl_uniquekey, 
            RecentProducts.website_name AS RecentProducts__website_name, 
            RecentProducts.original_price AS RecentProducts__original_price, 
            RecentProducts.discount_amount AS RecentProducts__discount_amount, 
            RecentProducts.description AS RecentProducts__description, 
            RecentProducts.img_url AS RecentProducts__img_url, 
            RecentProducts.total_review AS RecentProducts__total_review, 
            RecentProducts.review_average AS RecentProducts__review_average, 
            RecentProducts.price AS RecentProducts__price, 
            RecentProducts.percentage AS RecentProducts__percentage, 
            RecentProducts.expiry AS RecentProducts__expiry, 
            RecentProducts.price_after_discount AS RecentProducts__price_after_discount, 
            RecentProducts.price_zero_padded AS RecentProducts__price_zero_padded, 
            RecentProducts.created AS RecentProducts__created, 
            RecentProducts.modified AS RecentProducts__modified 
          FROM 
            products RecentProducts 
            INNER JOIN product_categories ProductCategories ON ProductCategories.id = (
              RecentProducts.product_category_id
            ) 
          WHERE 
            ProductCategories.category_id in (1, 2)
        ) __ranked__RecentProducts 
      WHERE 
        __ranked__RecentProducts.__row_number <= 9
    )
  ) 
ORDER BY 
  RecentProducts.created DESC

我的查询生成器代码

$categories = \Cake\ORM\TableRegistry::getTableLocator()->get( 'Categories' )->find()
        ->contain(
                    [
                        'CategoryKeywords',
                        'RecentProducts'
                    ]
         )

这里我得到 18 个项目,但期望是 9 个。

【问题讨论】:

  • 开箱即用不支持,根据具体用例可能会非常棘手:stackoverflow.com/questions/30241975/…
  • 表中有 ProductCategories 直接关系吗?
  • @RiTeSh 编号分类->产品分类->产品
  • @ndm 我已经尝试过你的解决方案,我面临的问题是如何获取嵌套容器表产品类别->产品类别->产品的限制数据
  • 只需像任何其他关联一样包含新关联,例如,您引用TopProducts(或您命名的任何名称)而不是Products

标签: cakephp cakephp-4.x


【解决方案1】:

假设你有以下关系

Categories有很多ProductCategories

ProductCategories有很多产品

产品属于ProductCategories

$this->loadModel('Products');
        $product = $this->Products->find('all')
            ->contain(['ProductCategories'])
            ->limit(20)
            ->groupBy('product_categories_id');

【讨论】:

  • 我想获取类别到产品,而不是产品到类别。
猜你喜欢
  • 2021-10-08
  • 2022-12-03
  • 2021-09-08
  • 2021-08-05
  • 2023-03-12
  • 1970-01-01
  • 2019-12-22
  • 2012-07-27
  • 1970-01-01
相关资源
最近更新 更多