CategoryModel.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. namespace addons\QiJuhe\common\models;
  3. use Yii;
  4. /**
  5. * This is the model class for table "qimall_addon_qijuhe_category".
  6. *
  7. * @property int $id
  8. * @property int|null $category_id 分类ID
  9. * @property int|null $supply_id 分类ID
  10. * @property string|null $name 分类名称
  11. * @property int|null $sort 排序
  12. * @property int|null $level 分类层级,从0开始
  13. * @property string|null $desc
  14. * @property int|null $is_display 1, 状态 1启用 0关闭
  15. * @property string|null $image
  16. * @property string|null $icon
  17. * @property int|null $parent_id 分类父id(对应三方分类id父id)
  18. * @property int|null $source 来源1自营2京东6阿里巴巴7天猫
  19. * @property int|null $created_at
  20. * @property int|null $updated_at
  21. */
  22. class CategoryModel extends \yii\db\ActiveRecord
  23. {
  24. /**
  25. * {@inheritdoc}
  26. */
  27. public static function tableName()
  28. {
  29. return '{{%addons_qijuhe_category}}';
  30. }
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public function rules()
  35. {
  36. return [
  37. [['category_id','supply_id', 'sort', 'level', 'is_display', 'parent_id', 'source', 'created_at', 'updated_at'], 'integer'],
  38. [['name', 'desc', 'image', 'icon'], 'string', 'max' => 255],
  39. ];
  40. }
  41. /**
  42. * {@inheritdoc}
  43. */
  44. public function attributeLabels()
  45. {
  46. return [
  47. 'id' => 'ID',
  48. 'supply_id' => 'supply id',
  49. 'category_id' => 'Category ID',
  50. 'name' => 'Name',
  51. 'sort' => 'Sort',
  52. 'level' => 'Level',
  53. 'desc' => 'Desc',
  54. 'is_display' => 'Is Display',
  55. 'image' => 'Image',
  56. 'icon' => 'Icon',
  57. 'parent_id' => 'Parent ID',
  58. 'source' => 'Source',
  59. 'created_at' => 'Created At',
  60. 'updated_at' => 'Updated At',
  61. ];
  62. }
  63. public static function getAllCateId(int $parent_id = 0,$field = 'category_id'):array
  64. {
  65. $model = self::find()->select($field);
  66. if($parent_id > 0){
  67. $model->where(['parent_id'=>$parent_id]);
  68. }
  69. $ids = $model->all();
  70. return empty($ids) ? [] : array_column($ids,$field,'category_id');
  71. }
  72. // 过滤字段
  73. public static function filterField(array $data):array {
  74. $safe_attr = self::getTableSchema()->getColumnNames();
  75. $not_safe_attr = array_diff(array_keys($data),$safe_attr);
  76. foreach ($not_safe_attr as $key) {
  77. unset($data[$key]);
  78. }
  79. return $data;
  80. }
  81. public static function updateCate(array $cate){
  82. $cate = self::filterField($cate);
  83. $cate['category_id'] = $cate['id'];
  84. unset($cate['id']);
  85. self::updateAll($cate,['category_id'=>$cate['category_id']]);
  86. }
  87. /**
  88. * 批量插入员工
  89. * @desc
  90. *
  91. * @param $user_list
  92. *
  93. * @return int
  94. * @throws \yii\db\Exception
  95. */
  96. public static function batchAdd($data){
  97. $field = array_keys($data[0]);
  98. $value = [];
  99. foreach ($data as $item) {
  100. $item['id'] = null;
  101. $value[] = array_values($item);
  102. }
  103. $result = \Yii::$app->db->createCommand()->batchInsert(self::tableName(),$field,$value)->execute();
  104. return $result;
  105. }
  106. }