| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- <?php
- /**
- * 厦门云联储网络科技有限公司
- * https://www.baokuaiyun.com
- * Copyright (c) 2023 爆块云 All rights reserved.
- */
- namespace app\models;
- use Yii;
- use yii\behaviors\TimestampBehavior;
- /**
- * This is the model class for table "{{%mch_cat}}".
- *
- * @property integer $id
- * @property integer $parent_id
- * @property integer $mch_id
- * @property string $name
- * @property string $icon
- * @property integer $sort
- * @property integer $is_delete
- * @property string $created_at
- * @property string $updated_at
- */
- class MchCat extends \yii\db\ActiveRecord
- {
- /**
- * @inheritdoc
- */
- public static function tableName()
- {
- return '{{%mch_cat}}';
- }
- public function behaviors()
- {
- return [
- [
- // 自动更新创建和更新时间
- 'class' => TimestampBehavior::class,
- 'value' => time()
- ]
- ];
- }
- /**
- * @inheritdoc
- */
- public function rules()
- {
- return [
- [['parent_id', 'mch_id', 'sort', 'is_delete', 'created_at', 'updated_at',], 'integer'],
- [['icon'], 'string'],
- [['name'], 'string', 'max' => 255],
- ];
- }
- /**
- * @inheritdoc
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'parent_id' => 'Parent ID',
- 'mch_id' => 'Mch ID',
- 'name' => '分类名称',
- 'icon' => '分类图标',
- 'sort' => 'Sort',
- 'is_delete' => 'Is Delete',
- 'created_at' => '创建时间',
- 'updated_at' => '更新时间'
- ];
- }
- /**
- * 获取所有的分类
- * @param int $store_id
- * @param int $parent_id
- * @return array
- */
- public static function getCatList($mch_id = 1, $is_show = -1, $parent_id = 0)
- {
- $res = [];
- $query = self::find()->where([
- 'parent_id' => $parent_id,
- 'is_delete' => 0,
- 'mch_id' => $mch_id
- ])->orderBy(['sort'=>SORT_DESC]);
- $cat_list = $query->asArray()->all();
- if (empty($cat_list)) {
- return $res;
- }
- foreach ($cat_list as &$val) {
- $val['children'] = self::getCatList($mch_id, $is_show, $val['id']);
- $res[] = $val;
- }
- return $res;
- }
- public static function getCatId($parent_id, &$cat_list = [])
- {
- $cat_list[] = $parent_id;
- $query = self::find()->where([
- 'parent_id' => $parent_id,
- 'is_delete' => 0,
- 'mch_id' => get_mch_id()
- ])->orderBy(['sort'=>SORT_DESC]);
- $cat = $query->asArray()->all();
- if (empty($cat)) {
- return $cat_list;
- }
- foreach ($cat as &$val) {
- self::getCatId($val['id'], $cat_list);
- }
- return $cat_list;
- }
- }
|