ProductCdkeyDao.php 4.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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\ProductCdkey as model;
  14. class ProductCdkeyDao extends BaseDao
  15. {
  16. protected function getModel(): string
  17. {
  18. return model::class;
  19. }
  20. /**
  21. * 根据条件搜索数据。
  22. *
  23. * 该方法通过动态构建查询条件来搜索数据库。它支持多个条件参数,
  24. * 并且只有在参数存在时才会添加相应的查询条件。这提高了查询的灵活性,
  25. * 允许根据不同的需求构建不同的查询。
  26. *
  27. * @param array $where 查询条件,包含多个可能的字段及其值。
  28. * @return \yii\db\Query 查询对象,可用于进一步的查询操作或获取结果。
  29. */
  30. public function search($where)
  31. {
  32. // 获取数据库连接对象
  33. $query = $this->getModel()::getDb();
  34. // 动态添加查询条件基于$where数组中的键值对
  35. $query
  36. ->when(isset($where['cdkey_id']) && $where['cdkey_id'] !== '', function ($query) use ($where) {
  37. // 如果'cdkey_id'存在,添加到查询条件中
  38. $query->where('cdkey_id', $where['cdkey_id']);
  39. })
  40. ->when(isset($where['cdkey_ids']) && $where['cdkey_ids'] !== '', function ($query) use ($where) {
  41. // 如果'cdkey_id'存在,添加到查询条件中
  42. $query->whereIn('cdkey_id', $where['cdkey_ids']);
  43. })
  44. ->when(isset($where['library_id']) && $where['library_id'] !== '', function ($query) use ($where) {
  45. // 如果'library_id'存在,添加到查询条件中
  46. $query->where('library_id', $where['library_id']);
  47. })
  48. ->when(isset($where['product_id']) && $where['product_id'] !== '', function ($query) use ($where) {
  49. // 如果'product_id'存在,添加到查询条件中
  50. $query->where('product_id', $where['product_id']);
  51. })
  52. ->when(isset($where['status']) && $where['status'] !== '', function ($query) use ($where) {
  53. // 如果'status'存在,添加到查询条件中
  54. $query->where('status', $where['status']);
  55. })
  56. ->when(isset($where['is_type']) && $where['is_type'] !== '', function ($query) use ($where) {
  57. // 如果'is_type'存在,添加到查询条件中
  58. $query->where('is_type', $where['is_type']);
  59. })
  60. ->when(isset($where['mer_id']) && $where['mer_id'] !== '', function ($query) use ($where) {
  61. // 如果'mer_id'存在,添加到查询条件中
  62. $query->where('mer_id', $where['mer_id']);
  63. })
  64. ->when(isset($where['keys']) && $where['keys'] !== '', function ($query) use ($where) {
  65. // 如果'keys'存在,使用其中的值作为查询条件
  66. $query->whereIn('key', $where['keys']);
  67. });
  68. // 返回构建好的查询对象
  69. return $query;
  70. }
  71. /**
  72. * 清除指定产品的属性
  73. *
  74. * 本函数用于删除数据库中与指定产品ID相关联的所有属性记录。
  75. * 通过调用关联模型的where方法来筛选出特定产品ID的属性记录,然后调用delete方法进行删除。
  76. *
  77. * @param int $productId 产品的ID,用于指定要清除属性的产品。
  78. * @return int 返回受影响的行数,即被删除的属性记录数。
  79. */
  80. public function clearAttr(int $productId)
  81. {
  82. // 使用动态模型删除与指定产品ID相关的所有属性记录
  83. return ($this->getModel())::where('product_id',$productId)->where('is_type',0)->delete();
  84. }
  85. }