Skip to content

Commit

Permalink
product services add function:getCategorysByProductIds()
Browse files Browse the repository at this point in the history
  • Loading branch information
fancyecommerce committed Apr 1, 2020
1 parent 555ede0 commit 5ae5515
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
21 changes: 21 additions & 0 deletions services/product/ProductMongodb.php
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,27 @@ public function getCategoryIdsByProductIds($product_ids)
return array_unique($categoryIds);
}

/**
* @param $product_ids | array, 产品id数组
* 通过产品id数组,得到产品对应的分类id数组
*/
public function getCategorysByProductIds($product_ids)
{
$coll = $this->_productModel->find()->asArray()
->where([
'in', '_id', $product_ids
])->all();
$categoryIds = [];
foreach ($coll as $one) {
$productId = (string)$one['_id'];
if (isset($one['category']) && !empty($one['category']) && is_array($one['category'])) {
$categoryIds[$productId] = $one['category'];
}
}

return $categoryIds;
}

/**
* @param $one|array
* 对保存的数据进行数据验证
Expand Down
24 changes: 24 additions & 0 deletions services/product/ProductMysqldb.php
Original file line number Diff line number Diff line change
Expand Up @@ -730,6 +730,30 @@ public function getCategoryIdsByProductIds($product_ids)
return array_unique($arr);
}

/**
* @param $product_ids | array, 产品id数组
* 通过产品id数组,得到产品对应的分类id数组
*/
public function getCategorysByProductIds($product_ids)
{
if (empty($product_ids) || !is_array($product_ids)) {
return [];
}
$coll = $this->_categoryProductModel->find()
->asArray()
->where([
'in', 'product_id', $product_ids
])->all();
$arr = [];
foreach ($coll as $one) {
$category_id = (int)$one['category_id'];
$product_id = (int)$one['product_id'];
$arr[$product_id][] = $category_id;
}

return $arr;
}

public function getProductIdsByCategoryId($category_id)
{
$coll = $this->_categoryProductModel->find()
Expand Down

0 comments on commit 5ae5515

Please sign in to comment.