【问题标题】:cakephp 3 matching not workingcakephp 3匹配不起作用
【发布时间】:2016-08-26 04:13:44
【问题描述】:

我有categoriesproductsseller_products 表,并希望从三个表中按类别分组,其中seller_products.stock > 0seller_products.status = 1

这是我的代码

$pros1 = $this->Products->Categories->find()
      ->where([
    ])
->select(['Categories.id', 'Categories.title'])
->distinct(['Categories.id'])
->contain(['Products'])
->matching('Products.SellerProducts', function(\Cake\ORM\Query $q) {
   return $q->where(['SellerProducts.stock >' => 0, 'SellerProducts.status' => 1]);
});

但这不起作用。我通过匹配获取所有产品按类别分组不起作用,并且仍然获得库存为 0 或状态不是 1 的产品

他们的关联是

$categories->hasMany('Products');
$products->hasMany('SellerProducts');

【问题讨论】:

  • 生成的查询是什么?

标签: cakephp matching model-associations cakephp-3.2


【解决方案1】:

您需要了解contain()matching() 是不同的。您正在询问具有有库存产品的类别。但是,如果您想要匹配的产品是什么,您还需要在contain() 中过滤它们:

$matcher = function(\Cake\ORM\Query $q) {
   return $q->where(['SellerProducts.stock >' => 0, 'SellerProducts.status' => 1]);
};


$pros1 = $this->Products->Categories->find()
    ->select(['Categories.id', 'Categories.title'])
    ->distinct(['Categories.id'])
    ->contain(['Products' => function ($q) use ($matcher) {
         return $q->matching('SellerProducts', $matcher);
    }])
    ->matching('Products.SellerProducts', $matcher);

【讨论】:

  • 如果删除matching 会发生什么?你会得到完全相同的结果吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-04-30
  • 1970-01-01
  • 1970-01-01
  • 2016-12-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多