cms update
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user