CakePHP:查询两次打印相同的值
CakePHP : query printing same values twice
我正在开发 CakePHP 3.2。
我有 categories
、products
、seller_products
table,它们的关联是
categories->hasMany('Products');
seller_products->hasMany('Products');
我必须按类别检索所有产品组,其中 seller_products.stock > 0
这就是我正在做的
$pros1 = $this->Products->Categories->find()
->where([
'Categories.status' => 0,
])
->contain([
'Products.SellerProducts', 'Subcategories', 'Subcategories.ProductTypes'
])
->matching('Products.SellerProducts', function(\Cake\ORM\Query $q) {
return $q->where(['SellerProducts.stock >' => 0]);
});
但在调试时,它两次返回相同的值。
foreach($pros1 as $p){debug($p);}
两次打印相同的值
/src/Controller/PagesController.php (line 120)
object(App\Model\Entity\Category) {
'id' => (int) 1,
'title' => 'Electronics',
'description' => '',
'icon' => 'fa-television',
'status' => (int) 0,
'created' => object(Cake\I18n\Time) {
.........
}
/src/Controller/PagesController.php (line 120)
object(App\Model\Entity\Category) {
'id' => (int) 1,
'title' => 'Electronics',
'description' => '',
'icon' => 'fa-television',
'status' => (int) 0,
'created' => object(Cake\I18n\Time) {
.........
}
和 debug(count($pros1))
打印 1
引自文档:
As this function will create an INNER JOIN
, you might want to consider
calling distinct
on the find query as you might get duplicate rows if
your conditions don’t exclude them already. This might be the case,
for example, when the same users comments more than once on a single
article.
Cookbook > Database Access & ORM > Query Builder > Filtering by Associated Data
在您的案例中,有多个属于同一类别的匹配产品。因此,要么对表主键使用不同的 select,要么按它分组 (there may be no difference between the two)。
$pros1 = $this->Products->Categories
->find()
->distinct('Categories.id')
// ...
// ...
->matching(/* ... */)
->group('Categories.id');
另见
我正在开发 CakePHP 3.2。
我有 categories
、products
、seller_products
table,它们的关联是
categories->hasMany('Products');
seller_products->hasMany('Products');
我必须按类别检索所有产品组,其中 seller_products.stock > 0
这就是我正在做的
$pros1 = $this->Products->Categories->find()
->where([
'Categories.status' => 0,
])
->contain([
'Products.SellerProducts', 'Subcategories', 'Subcategories.ProductTypes'
])
->matching('Products.SellerProducts', function(\Cake\ORM\Query $q) {
return $q->where(['SellerProducts.stock >' => 0]);
});
但在调试时,它两次返回相同的值。
foreach($pros1 as $p){debug($p);}
两次打印相同的值
/src/Controller/PagesController.php (line 120)
object(App\Model\Entity\Category) {
'id' => (int) 1,
'title' => 'Electronics',
'description' => '',
'icon' => 'fa-television',
'status' => (int) 0,
'created' => object(Cake\I18n\Time) {
.........
}
/src/Controller/PagesController.php (line 120)
object(App\Model\Entity\Category) {
'id' => (int) 1,
'title' => 'Electronics',
'description' => '',
'icon' => 'fa-television',
'status' => (int) 0,
'created' => object(Cake\I18n\Time) {
.........
}
和 debug(count($pros1))
打印 1
引自文档:
As this function will create an
INNER JOIN
, you might want to consider callingdistinct
on the find query as you might get duplicate rows if your conditions don’t exclude them already. This might be the case, for example, when the same users comments more than once on a single article.
Cookbook > Database Access & ORM > Query Builder > Filtering by Associated Data
在您的案例中,有多个属于同一类别的匹配产品。因此,要么对表主键使用不同的 select,要么按它分组 (there may be no difference between the two)。
$pros1 = $this->Products->Categories
->find()
->distinct('Categories.id')
// ...
// ...
->matching(/* ... */)
->group('Categories.id');
另见