123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270 |
- <?php
- namespace common\models;
- use common\behaviors\PositionBehavior;
- use common\behaviors\CacheInvalidateBehavior;
- use common\behaviors\MetaBehavior;
- use common\behaviors\SoftDeleteBehavior;
- use common\enums\StatusEnum;
- use common\helpers\Tree;
- use common\models\behaviors\CategoryBehavior;
- use common\models\query\CategoryQuery;
- use Yii;
- use yii\behaviors\TimestampBehavior;
- use yii\caching\TagDependency;
- use yii\db\Expression;
- /**
- * This is the model class for table "{{%category}}".
- *
- * @property int $id
- * @property int $pid
- * @property string $title
- * @property string $slug
- * @property string $description
- * @property integer $status
- * @property int $created_at
- * @property int $updated_at
- * @property string $module
- * @property string $cover
- * @property int $allow_publish
- * @property int $type
- * @property int $is_del
- */
- class Category extends \yii\db\ActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%category}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['title'], 'required'],
- //中文没法自动生成slug,又没必要必填
- [
- 'slug', 'default', 'value' => function ($model) {
- return $model->title;
- }
- ],
- ['module', 'safe'],
- [['pid', 'sort', 'allow_publish', 'type', 'status'], 'integer'],
- ['pid', 'default', 'value' => 0],
- [['sort', 'is_del'], 'default', 'value' => 0],
- ['status', 'default', 'value' => StatusEnum::STATUS_ON],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'title' => '名称',
- 'slug' => '标识',
- 'pid' => '上级分类',
- 'ptitle' => '上级分类', // 非表字段,方便后台显示
- 'description' => '分类介绍',
- 'article' => '文章数', //冗余字段,方便查询
- 'sort' => '排序',
- 'module' => '支持的人才模型',
- 'allow_publish' => '是否允许发布内容',
- 'type' => '类型',
- 'created_at' => '创建时间',
- 'updated_at' => '更新时间',
- 'status' => Yii::t('common', '状态'),
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function behaviors()
- {
- return [
- [
- 'class' => SoftDeleteBehavior::className(),
- 'softDeleteAttributeValues' => [
- 'updated_at' => time(),
- 'is_del' => StatusEnum::STATUS_ON,
- 'status' => StatusEnum::STATUS_OFF,
- ],
- 'invokeDeleteEvents' => false // 不触发删除相关事件
- ],
- TimestampBehavior::className(),
- [
- 'class' => MetaBehavior::className(),
- ],
- CategoryBehavior::className(),
- 'positionBehavior' => [
- 'class' => PositionBehavior::className(),
- 'positionAttribute' => 'sort',
- 'groupAttributes' => [
- 'pid'
- ],
- ],
- [
- 'class' => CacheInvalidateBehavior::className(),
- 'tags' => [
- 'categoryList'
- ]
- ]
- ];
- }
- public function getMetaData()
- {
- $model = $this->getMetaModel();
- $title = $model->title ?: $this->title;
- $keywords = $model->keywords;
- $description = $model->description ?: $this->description;
- return [$title, $keywords, $description];
- }
- /**
- * 获取分类名
- */
- public function getPtitle()
- {
- return static::find()->select('title')->where(['id' => $this->pid])->scalar();
- }
- public static function lists($module = null)
- {
- $list = Yii::$app->cache->get(['categoryList', $module]);
- if ($list === false) {
- $query = static::find()->active();
- if ($module) {
- $query->andWhere(new Expression("FIND_IN_SET('{$module}', module) "));
- }
- $list = $query->asArray()->all();
- Yii::$app->cache->set(['categoryList', $module], $list, 0, new TagDependency(['tags' => ['categoryList']]));
- }
- return $list;
- }
- public static function listsByType($type = null)
- {
- $list = Yii::$app->cache->get(['categoryListByType', $type]);
- if ($list === false) {
- $query = static::find()->active();
- if (isset($type)) {
- $query->andWhere(['type' => $type]);
- }
- $list = $query->asArray()->all();
- Yii::$app->cache->set(['categoryListByType', $type], $list, 0, new TagDependency(['tags' => ['categoryListByType']]));
- }
- return $list;
- }
- public static function getDropDownList($tree = [], &$result = [], $deep = 0, $separator = '--')
- {
- $deep++;
- foreach ($tree as $list) {
- $result[$list['id']] = str_repeat($separator, $deep - 1) . $list['title'];
- if (isset($list['children'])) {
- self::getDropDownList($list['children'], $result, $deep);
- }
- }
- return $result;
- }
- public function getCategoryNameById($id)
- {
- $list = $this->lists();
- return isset($list[$id]) ? $list[$id] : null;
- }
- public static function getIdByName($name)
- {
- $list = self::lists();
- return array_search($name, $list);
- }
- public static function findByIdOrSlug($id)
- {
- if (intval($id) == 0) {
- $condition = ["slug" => $id];
- } else {
- $condition = [
- $id
- ];
- }
- return static::findOne($condition);
- }
- public static function getAllowPublishEnum()
- {
- return [
- '不允许',
- '只允许后台',
- '允许前后台'
- ];
- }
- public static function getTypeEnum()
- {
- return [
- '分类',
- '产业链',
- ];
- }
- public function beforeSave($insert)
- {
- if (parent::beforeSave($insert)) {
- if (!empty($this->module)) {
- $this->module = implode(',', $this->module);
- }
- return true;
- } else {
- return false;
- }
- }
- public function afterSave($insert, $changedAttributes)
- {
- parent::afterSave($insert, $changedAttributes);
- // Yii::$app->getCache()->delete('categoryList');
- // Yii::$app->getCache()->delete('categoryListByType');
- Yii::$app->cache->flush();
- return true;
- }
- public function afterFind()
- {
- parent::afterFind();
- $this->module = explode(',', $this->module);
- }
- public function renderModule($separator = ',')
- {
- return join($separator, array_map(function ($val) {
- return array_get(ArticleModule::getTypeEnum(), $val);
- }, $this->module));
- }
- /**
- * @inheritdoc
- * @return CategoryQuery the newly created [[ActiveQuery]] instance.
- */
- public static function find()
- {
- return Yii::createObject(CategoryQuery::className(), [get_called_class()]);
- }
- }
|