cakephp 3匹配不工作
我有cakephp 3匹配不工作,cakephp,matching,model-associations,cakephp-3.2,Cakephp,Matching,Model Associations,Cakephp 3.2,我有类别,产品和卖方产品表,并希望从三个表中按类别分组选择全部,其中卖方产品.stock>0和卖方产品.status=1 这是我的密码 $pros1 = $this->Products->Categories->find() ->where([ ]) ->select(['Categories.id', 'Categories.title']) ->distinct(['Categories.id']) ->contain(['Pro
类别
,产品
和卖方产品
表,并希望从三个表中按类别分组选择全部,其中卖方产品.stock>0
和卖方产品.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');
您需要了解
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);
生成的查询是什么?如果删除匹配的
会发生什么?你会得到完全相同的结果吗?