cms update
This commit is contained in:
parent
5abc675e5d
commit
034705bb3d
@ -0,0 +1,44 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | CatchAdmin [Just Like ~ ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2017~{$year} http://catchadmin.com All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: JaguarJack [ njphper@gmail.com ]
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
use think\migration\Migrator;
|
||||
use think\migration\db\Column;
|
||||
|
||||
class RemoveArticlesTagsField extends Migrator
|
||||
{
|
||||
/**
|
||||
* Change Method.
|
||||
*
|
||||
* Write your reversible migrations using this method.
|
||||
*
|
||||
* More information on writing migrations is available here:
|
||||
* http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
|
||||
*
|
||||
* The following commands can be used in this method and Phinx will
|
||||
* automatically reverse them when rolling back:
|
||||
*
|
||||
* createTable
|
||||
* renameTable
|
||||
* addColumn
|
||||
* renameColumn
|
||||
* addIndex
|
||||
* addForeignKey
|
||||
*
|
||||
* Remember to call "create()" or "update()" and NOT "save()" when working
|
||||
* with the Table class.
|
||||
*/
|
||||
public function change()
|
||||
{
|
||||
if ($this->hasTable('cms_articles')) {
|
||||
$this->table('cms_articles')->removeColumn('tags');
|
||||
}
|
||||
}
|
||||
}
|
@ -15,6 +15,36 @@ namespace catchAdmin\cms\model;
|
||||
|
||||
use catchAdmin\cms\model\events\ArticlesEvent;
|
||||
|
||||
/**
|
||||
* Class Articles
|
||||
* @package catchAdmin\cms\model
|
||||
* @auth CatchAdmin
|
||||
* @time 2021年05月22日
|
||||
*
|
||||
* @property Articles category_id
|
||||
* @property Articles id
|
||||
* @property Articles title
|
||||
* @property Articles cover
|
||||
* @property Articles images
|
||||
* @property Articles tags
|
||||
* @property Articles url
|
||||
* @property Articles content
|
||||
* @property Articles keywords
|
||||
* @property Articles description
|
||||
* @property Articles pv
|
||||
* @property Articles likes
|
||||
* @property Articles comments
|
||||
* @property Articles is_top
|
||||
* @property Articles is_recommend
|
||||
* @property Articles status
|
||||
* @property Articles weight
|
||||
* @property Articles is_can_comment
|
||||
* @property Articles creator_id
|
||||
* @property Articles created_at
|
||||
* @property Articles updated_at
|
||||
* @property Articles deleted_at
|
||||
*
|
||||
*/
|
||||
class Articles extends BaseModel
|
||||
{
|
||||
use ArticlesEvent;
|
||||
@ -31,8 +61,6 @@ class Articles extends BaseModel
|
||||
'cover', // 封面
|
||||
// 多图集合
|
||||
'images',
|
||||
// 标签集合
|
||||
'tags',
|
||||
// 自定义URL
|
||||
'url',
|
||||
// 内容
|
||||
@ -75,6 +103,44 @@ class Articles extends BaseModel
|
||||
const CAN_COMMENT = 1; // 评论允许
|
||||
const UN_CAN_COMMENT = 2; // 评论不允许
|
||||
|
||||
/**
|
||||
* 列表
|
||||
*
|
||||
* @auth CatchAdmin
|
||||
* @time 2021年05月22日
|
||||
* @return mixed
|
||||
*/
|
||||
public function getList()
|
||||
{
|
||||
// 分页列表
|
||||
return $this->catchSearch()
|
||||
->field($this->aliasField('*'))
|
||||
->catchJoin(Category::class, 'id', 'category_id', ['name as category'])
|
||||
->catchOrder()
|
||||
->creator()
|
||||
->paginate();
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询
|
||||
*
|
||||
* @auth CatchAdmin
|
||||
* @time 2021年05月22日
|
||||
* @param $id
|
||||
* @param array|string[] $field
|
||||
* @param bool $trash
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
* @return array|mixed|\think\Model|null
|
||||
*/
|
||||
public function findBy($id, array $field = ['*'], bool $trash = false)
|
||||
{
|
||||
return $this->where('id', $id)
|
||||
->with(['tag'])
|
||||
->field('*')
|
||||
->find();
|
||||
}
|
||||
|
||||
/**
|
||||
* 文章标签
|
||||
@ -82,10 +148,37 @@ class Articles extends BaseModel
|
||||
* @time 2021年05月17日
|
||||
* @return \think\model\relation\BelongsToMany
|
||||
*/
|
||||
public function tags(): \think\model\relation\BelongsToMany
|
||||
public function tag(): \think\model\relation\BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(Tags::class, 'cms_article_relate_tags',
|
||||
'tag_id', 'article_id'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除标签
|
||||
*
|
||||
* @param array $ids
|
||||
* @return int
|
||||
* @author CatchAdmin
|
||||
* @time 2021年05月22日
|
||||
*/
|
||||
public function detachTags(array $ids = []): int
|
||||
{
|
||||
return $this->tag()->detach($ids);
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增标签
|
||||
*
|
||||
* @author CatchAdmin
|
||||
* @time 2021年05月22日
|
||||
* @param array $ids
|
||||
* @throws \think\db\exception\DbException
|
||||
* @return array|\think\model\Pivot
|
||||
*/
|
||||
public function attachTags(array $ids)
|
||||
{
|
||||
return $this->tag()->attach($ids);
|
||||
}
|
||||
}
|
@ -2,6 +2,7 @@
|
||||
namespace catchAdmin\cms\model\events;
|
||||
|
||||
use catchAdmin\cms\model\Articles;
|
||||
use catchAdmin\cms\model\BaseModel;
|
||||
use catchAdmin\cms\model\Tags;
|
||||
use catcher\exceptions\FailedException;
|
||||
use catcher\Utils;
|
||||
@ -17,11 +18,7 @@ trait ArticlesEvent
|
||||
*/
|
||||
public static function onBeforeInsert(Articles $model)
|
||||
{
|
||||
$model->category_id = $model->category_id[0];
|
||||
|
||||
$model->images = implode(',', $model->images);
|
||||
|
||||
$model->tags = implode(',', $model->tags);
|
||||
self::beforeChangeData($model);
|
||||
}
|
||||
|
||||
|
||||
@ -36,23 +33,100 @@ trait ArticlesEvent
|
||||
* @return void
|
||||
*/
|
||||
public static function onAfterInsert(Articles $model)
|
||||
{
|
||||
$model->attachTags(self::getTagsId($model));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @auth CatchAdmin
|
||||
* @time 2021年05月22日
|
||||
* @param Articles $model
|
||||
* @return void
|
||||
*/
|
||||
public static function onBeforeUpdate(Articles $model)
|
||||
{
|
||||
self::beforeChangeData($model);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @auth CatchAdmin
|
||||
* @time 2021年05月22日
|
||||
* @param Articles $model
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
* @return void
|
||||
*/
|
||||
public static function onAfterUpdate(Articles $model)
|
||||
{
|
||||
$data = $model->getData();
|
||||
|
||||
if (isset($data['tags'])) {
|
||||
|
||||
$tagIds = self::getTagsId($model);
|
||||
|
||||
$article = $model->where($model->getWhere())->find();
|
||||
|
||||
$article->detachTags($article->tag()->select()->column('id'));
|
||||
|
||||
$article->attachTags(self::getTagsId($model));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 插入前
|
||||
*
|
||||
* @auth CatchAdmin
|
||||
* @time 2021年05月22日
|
||||
* @param $model
|
||||
* @return void
|
||||
*/
|
||||
protected static function beforeChangeData($model)
|
||||
{
|
||||
$data = $model->getData();
|
||||
|
||||
if (isset($data['category_id'])) {
|
||||
$model->category_id = $model->category_id[count($model->category_id) - 1];
|
||||
}
|
||||
|
||||
if (isset($data['images'])) {
|
||||
$model->images = empty($model->images) ? '' : implode(',', $model->images);
|
||||
}
|
||||
|
||||
if (isset($data['tags'])) {
|
||||
$model->tags = empty($model->tags) ? '' : implode(',', $model->tags);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 插入后
|
||||
*
|
||||
* @auth CatchAdmin
|
||||
* @time 2021年05月22日
|
||||
* @param $model
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
* @return array
|
||||
*/
|
||||
protected static function getTagsId($model): array
|
||||
{
|
||||
$tags = Utils::stringToArrayBy($model->tags);
|
||||
|
||||
$tagModel = new Tags;
|
||||
$existTags = $tagModel->whereIn('name', $tags)->select()->toArray();
|
||||
$existTagsName = array_column($existTags, 'name');
|
||||
|
||||
$tagsIds = [];
|
||||
$tagIds = [];
|
||||
foreach ($tags as $tag) {
|
||||
if (! in_array($tag, $existTagsName)) {
|
||||
$tagsIds[] = $tagModel->createBy([
|
||||
$tagModel = Tags::where('name', $tag)->findOrEmpty();
|
||||
|
||||
if ($tagModel->isEmpty()) {
|
||||
$tagIds[] = $tagModel->storeBy([
|
||||
'name' => $tag
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
$model->tags()->attach(array_merge(array_column($existTags, 'id'), $tagsIds));
|
||||
return array_merge(Tags::whereIn('name', $tags)->column('id'), $tagIds);
|
||||
}
|
||||
}
|
@ -25,9 +25,9 @@ trait CategoryEvent
|
||||
* @param \think\Model $category
|
||||
* @return void
|
||||
*/
|
||||
public static function onBeforeInsert($category): void
|
||||
public static function onBeforeInsert(\think\Model $category): void
|
||||
{
|
||||
$category->data(self::changeData($category->getData()));
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@ -37,9 +37,9 @@ trait CategoryEvent
|
||||
* @param \think\Model $category
|
||||
* @return mixed|void
|
||||
*/
|
||||
public static function onBeforeUpdate($category)
|
||||
public static function onBeforeUpdate(\think\Model $category)
|
||||
{
|
||||
// $category->data(self::changeData($category->getData()));
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@ -55,29 +55,4 @@ trait CategoryEvent
|
||||
throw new FailedException('存在下级栏目, 无法删除');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新Data
|
||||
*
|
||||
* @time 2021年03月03日
|
||||
* @param $data
|
||||
* @return mixed
|
||||
*/
|
||||
protected static function changeData($data)
|
||||
{
|
||||
/**
|
||||
if (isset($data['parent_id'])) {
|
||||
$parentId = $data['parent_id'];
|
||||
|
||||
if (!is_array($parentId)) {
|
||||
// $parentId = Utils::dealWithFormArrayString($parentId);
|
||||
}
|
||||
|
||||
$parentId = count($parentId) ? array_pop($parentId) : 0;
|
||||
|
||||
$data['parent_id'] = $parentId;
|
||||
}
|
||||
|
||||
return $data;*/
|
||||
}
|
||||
}
|
@ -13,17 +13,23 @@ class Articles extends CatchTable
|
||||
// TODO: Implement table() method.
|
||||
return $this->getTable('articles')
|
||||
->header([
|
||||
HeaderItem::label('编号')->prop('id'),
|
||||
HeaderItem::label('编号')->prop('id')->width(50),
|
||||
|
||||
HeaderItem::label('栏目')->prop('category'),
|
||||
HeaderItem::label('栏目')->prop('category')->width(100),
|
||||
|
||||
HeaderItem::label('标题')->prop('title'),
|
||||
|
||||
HeaderItem::label('权重')->prop('weight')->withEditNumberComponent(),
|
||||
HeaderItem::label('创建人')->prop('creator')->width(100),
|
||||
|
||||
HeaderItem::label('状态')->prop('status')->withSwitchComponent(),
|
||||
HeaderItem::label('权重')->prop('weight')->withEditNumberComponent()->width(100),
|
||||
|
||||
HeaderItem::label('创建时间')->prop('created_at'),
|
||||
HeaderItem::label('置顶')->prop('is_top')->withSwitchComponent()->width(100),
|
||||
|
||||
HeaderItem::label('状态')->prop('status')->withSwitchComponent()->width(100),
|
||||
|
||||
HeaderItem::label('推荐')->prop('is_recommend')->withSwitchComponent()->width(100),
|
||||
|
||||
HeaderItem::label('创建时间')->prop('created_at')->width(150),
|
||||
|
||||
HeaderItem::label('操作')->actions([
|
||||
Actions::update()->to('/cms/articles/detail'),
|
||||
|
@ -2,31 +2,36 @@
|
||||
namespace catchAdmin\cms\tables\forms;
|
||||
|
||||
use catchAdmin\cms\model\Tags;
|
||||
use catcher\library\form\Form;
|
||||
use catchAdmin\cms\model\Articles as Article;
|
||||
use catchAdmin\cms\model\Category;
|
||||
|
||||
class Articles extends Form
|
||||
class Articles extends BaseForm
|
||||
{
|
||||
protected $table = 'cms_articles';
|
||||
|
||||
public function fields(): array
|
||||
{
|
||||
$categories = Category::field(['id', 'name', 'parent_id'])->select()->toTree();
|
||||
|
||||
// TODO: Implement fields() method.
|
||||
return [
|
||||
self::input('title', '标题')->required()->maxlength(100)->col(12),
|
||||
|
||||
self::cascader('category_id', '选择分类')
|
||||
->options(
|
||||
Category::field(['id', 'name', 'parent_id'])->select()->toTree()
|
||||
)
|
||||
->col(12)
|
||||
->props(self::props('name', 'id', [
|
||||
'checkStrictly' => true
|
||||
]))
|
||||
->filterable(true)
|
||||
self::cascader('category_id', '选择分类', [])
|
||||
->options($categories)
|
||||
->clearable(true)
|
||||
->style(['width' => '100%'])
|
||||
->filterable(true)
|
||||
->showAllLevels(false)
|
||||
->props([
|
||||
'props' => [
|
||||
'value' => 'id',
|
||||
'label' => 'name',
|
||||
'checkStrictly' => true
|
||||
],
|
||||
])->style(['width' => '100%'])
|
||||
->required()->col(8),
|
||||
|
||||
|
||||
self::input('keywords', '关键字')->col(12),
|
||||
|
||||
self::image('封面', 'cover')->col(12),
|
||||
|
@ -4,6 +4,7 @@ namespace catchAdmin\cms\tables\forms;
|
||||
use catchAdmin\cms\model\Category as CategoryModel;
|
||||
use catchAdmin\cms\support\DynamicFormFields;
|
||||
use catcher\library\form\Form;
|
||||
use catcher\Utils;
|
||||
|
||||
abstract class BaseForm extends Form
|
||||
{
|
||||
@ -14,7 +15,7 @@ abstract class BaseForm extends Form
|
||||
$fields = parent::create(); // TODO: Change the autogenerated stub
|
||||
|
||||
if ($this->table) {
|
||||
return array_merge($fields, (new DynamicFormFields())->build($this->table));
|
||||
return array_merge($fields, (new DynamicFormFields())->build(Utils::tableWithPrefix($this->table)));
|
||||
}
|
||||
|
||||
return $fields;
|
||||
|
@ -46,7 +46,7 @@ class ModelUsedFields extends Form
|
||||
*/
|
||||
protected function getModelFields($tableName)
|
||||
{
|
||||
$fields = array_keys(Db::name($tableName)->getFields());
|
||||
$fields = array_keys(Db::table($tableName)->getFields());
|
||||
|
||||
foreach ($fields as $k => $field) {
|
||||
if ($field === app(Models::class)->getDeleteAtField()) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user