| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- <?php
- namespace addons\QiJuhe\common\models;
- use Yii;
- /**
- * This is the model class for table "qimall_addon_qijuhe_category".
- *
- * @property int $id
- * @property int|null $category_id 分类ID
- * @property int|null $supply_id 分类ID
- * @property string|null $name 分类名称
- * @property int|null $sort 排序
- * @property int|null $level 分类层级,从0开始
- * @property string|null $desc
- * @property int|null $is_display 1, 状态 1启用 0关闭
- * @property string|null $image
- * @property string|null $icon
- * @property int|null $parent_id 分类父id(对应三方分类id父id)
- * @property int|null $source 来源1自营2京东6阿里巴巴7天猫
- * @property int|null $created_at
- * @property int|null $updated_at
- */
- class CategoryModel extends \yii\db\ActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_qijuhe_category}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['category_id','supply_id', 'sort', 'level', 'is_display', 'parent_id', 'source', 'created_at', 'updated_at'], 'integer'],
- [['name', 'desc', 'image', 'icon'], 'string', 'max' => 255],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'supply_id' => 'supply id',
- 'category_id' => 'Category ID',
- 'name' => 'Name',
- 'sort' => 'Sort',
- 'level' => 'Level',
- 'desc' => 'Desc',
- 'is_display' => 'Is Display',
- 'image' => 'Image',
- 'icon' => 'Icon',
- 'parent_id' => 'Parent ID',
- 'source' => 'Source',
- 'created_at' => 'Created At',
- 'updated_at' => 'Updated At',
- ];
- }
- public static function getAllCateId(int $parent_id = 0,$field = 'category_id'):array
- {
- $model = self::find()->select($field);
- if($parent_id > 0){
- $model->where(['parent_id'=>$parent_id]);
- }
- $ids = $model->all();
- return empty($ids) ? [] : array_column($ids,$field,'category_id');
- }
- // 过滤字段
- public static function filterField(array $data):array {
- $safe_attr = self::getTableSchema()->getColumnNames();
- $not_safe_attr = array_diff(array_keys($data),$safe_attr);
- foreach ($not_safe_attr as $key) {
- unset($data[$key]);
- }
- return $data;
- }
- public static function updateCate(array $cate){
- $cate = self::filterField($cate);
- $cate['category_id'] = $cate['id'];
- unset($cate['id']);
- self::updateAll($cate,['category_id'=>$cate['category_id']]);
- }
- /**
- * 批量插入员工
- * @desc
- *
- * @param $user_list
- *
- * @return int
- * @throws \yii\db\Exception
- */
- public static function batchAdd($data){
- $field = array_keys($data[0]);
- $value = [];
- foreach ($data as $item) {
- $item['id'] = null;
- $value[] = array_values($item);
- }
- $result = \Yii::$app->db->createCommand()->batchInsert(self::tableName(),$field,$value)->execute();
- return $result;
- }
- }
|