ProductAttrDao.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\common\dao\store\product;
  12. use app\common\dao\BaseDao;
  13. use app\common\model\store\product\ProductAttr as model;
  14. class ProductAttrDao extends BaseDao
  15. {
  16. protected function getModel(): string
  17. {
  18. return model::class;
  19. }
  20. /**
  21. * 根据条件搜索数据库。
  22. *
  23. * 本函数旨在根据传入的条件参数,构建并返回一个数据库查询查询对象。
  24. * 它允许条件参数中的`product_id`来限定查询结果,只有当`product_id`被明确设置且非空时,才会添加相应的查询条件。
  25. *
  26. * @param array $where 查询条件数组,可能包含多个条件,本函数处理其中的`product_id`条件。
  27. * @return \yii\db\Query 查询对象,可用于进一步的查询操作或直接执行查询。
  28. */
  29. public function search($where)
  30. {
  31. // 获取模型对应的数据库查询对象
  32. $query = $this->getModel()::getDb();
  33. // 当`product_id`存在于条件数组中且非空时,添加相应的查询条件
  34. $query->when(isset($where['product_id']) && $where['product_id'] != '', function($query) use($where) {
  35. $query->where('product_id',$where['product_id']);
  36. });
  37. // 返回构建好的查询对象
  38. return $query;
  39. }
  40. /**
  41. * 清除指定产品的属性
  42. *
  43. * 本函数用于删除数据库中与指定产品ID相关联的所有属性记录。
  44. * 通过调用关联模型的where方法来筛选出特定产品ID的属性记录,然后调用delete方法进行删除。
  45. *
  46. * @param int $productId 产品的ID,用于指定要清除属性的产品。
  47. * @return int 返回受影响的行数,即被删除的属性记录数。
  48. */
  49. public function clearAttr(int $productId)
  50. {
  51. // 使用动态模型删除与指定产品ID相关的所有属性记录
  52. return ($this->getModel())::where('product_id',$productId)->delete();
  53. }
  54. /**
  55. * 插入数据到数据库。
  56. *
  57. * 本函数用于将给定的数据数组批量插入到数据库。它首先通过getModel方法获取模型对象,
  58. * 然后调用该对象的getDB方法来获取数据库连接对象,最后通过调用insertAll方法来执行数据插入操作。
  59. *
  60. * @param array $data 包含多条待插入数据的数组,每条数据是一个子数组。
  61. * @return mixed 返回数据库操作的结果。具体类型取决于数据库库的实现。
  62. */
  63. public function insert(array $data)
  64. {
  65. // 通过模型获取数据库对象,并执行批量插入操作
  66. return ($this->getModel()::getDB())->insertAll($data);
  67. }
  68. }