| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- <?php
- namespace common\models\link;
- use common\enums\StatusEnum;
- use Yii;
- /**
- * This is the model class for table "{{%link_category}}".
- *
- *
- * @property int $id 主键
- * @property string $name 分类key
- * @property string $title 中文名
- * @property string $pid 父级ID
- * @property string $is_list 是否是列表数据
- * @property string $remark 注意事项
- * @property int|null $status 状态[-1:删除;0:禁用;1启用]
- * @property int|null $created_at 创建时间
- * @property int|null $updated_at 修改时间
- */
- class LinkCategory extends \common\models\base\BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%link_category}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['status', 'is_list', 'created_at', 'updated_at'], 'integer'],
- [['title'], 'string', 'max' => 20],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => '主键',
- 'name' => '分类key',
- 'pid' => '父级ID',
- 'title' => '中文名',
- 'is_list' => '是否是列表数据',
- 'remark' => '注意事项',
- 'status' => '状态[-1:删除;0:禁用;1启用]',
- 'created_at' => '创建时间',
- 'updated_at' => '修改时间',
- ];
- }
- /**
- * 获取分类名称
- * @param $mall_id
- * @return array
- */
- public static function getCategoryName($is_format = true)
- {
- $data = [];
- $field = "id,name,title,remark";
- $list = self::find()->select($field)->where(['status' => StatusEnum::ENABLED])->asArray()->all();
- if ($is_format) {
- foreach ($list as $item) {
- $data[$item['id']] = $item['title'];
- }
- } else {
- $data = $list;
- }
- return $data;
- }
- /**
- * 获取树形结构
- * @Author: lun
- * @DateTime: 2021/10/13 0013 9:17
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param array $menu
- * @param int $pid
- * @return array
- */
- public static function getTreeMenu(array $menu, int $pid = 0)
- {
- $tree = [];
- foreach ($menu as $k => $v) {
- $v['value'] = $v['id'];
- $v['label'] = $v['title'];
- if ($v['pid'] == $pid) {
- $tmp = $v;
- // 当前路由的菜单被选中
- $children = self::getTreeMenu($menu, $v['value']);
- $children && $tmp['children'] = $children;
- $tree[] = $tmp;
- }
- }
- return $tree;
- }
- /**
- * 保存数据
- * @Author: lun
- * @DateTime: 2021/10/13 0013 18:56
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param $params
- * @return bool
- */
- public static function setData($params)
- {
- try{
- if (!empty($params['id'])) {
- $model = self::findOne(['id' => $params['id'], 'status' => [StatusEnum::ENABLED]]);
- if (empty($model)) throw new \Exception('分类不存在或已删除');
- } else {
- $model = new self();
- $model->loadDefaultValues();
- }
- $model->name = $params['name'];
- $model->title = $params['title'];
- $model->pid = $params['pid'];
- $model->is_list = $params['is_list'];
- $model->remark = $params['remark'];
- if(!$model->save(false)){
- throw new \Exception($model->getErrorMessage());
- }
- return true;
- }catch(\Exception $e){
- self::$static_error = $e->getMessage();
- return false;
- }
- }
- /**
- * 获取分类列表
- * @Author: lun
- * @DateTime: 2021/10/13 0013 9:34
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param array $cond
- * @param array $with
- * @param bool $is_array
- * @param string $select
- * @return array|\yii\db\ActiveRecord[]
- */
- public static function getLinkCategory(array $cond=[],array $with=[],bool $is_array=true,string $select='*'){
- $default_cond = [['<>','status',StatusEnum::DELETE]];
- $cond = array_merge($default_cond,$cond);
- $query = self::find()->select($select);
- self::paramPack(['where'=>$cond,'with'=>$with],$query);
- $data = $query->asArray($is_array)->all();
- return $data;
- }
- }
|