1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- <?php
- declare(strict_types=1);
- namespace app\model;
- use think\facade\Db;
- use think\facade\Config;
- use app\model\Base;
- use app\model\Tag;
- class TagArticle extends Base
- {
- protected $pk = 'id';
- protected $schema = [
- 'id' => "int", // 主键id
- "tid" => "int", // 标签id
- "aid" => "int", // 文章id
- "cid" => "int", // 栏目id
- ];
- protected $autoWriteTimestamp = false;
- public function article()
- {
- return $this->belongsTo('Article', 'aid')->bind(['id', 'title', 'titlepic', 'summary', 'hits', 'create_time', 'username']);
- }
- public function category()
- {
- return $this->belongsTo('Category', 'cid')->bind(['category_url' => 'url', 'category_name' => 'name']);
- }
- public static function queryList($tagid)
- {
- $limit = (int) Config::get('app.page_size', 20);
- return self::with(['article', 'category'])->where('tid', $tagid)->order('aid DESC')->limit($limit)->paginate();
- }
- public static function saveArticleTag(array $tags, int $aid, int $cid)
- {
- foreach ($tags as $tagname) {
- $tag = Tag::where('tagname', $tagname)->findOrEmpty();
- if ($tag->isEmpty()) {
- $tag->tagname = $tagname;
- }
- $tag->nums += 1;
- $tag->save();
- self::create([
- 'tid' => $tag->id,
- 'aid' => $aid,
- 'cid' => $cid
- ]);
- }
- }
- public static function delArticleTag(array $tags, int $aid)
- {
- foreach ($tags as $tagname) {
- $tag = Tag::where('tagname', $tagname)->findOrEmpty();
- if ($tag != null) {
- $tag->nums = $tag->nums - 1;
-
- $tag->nums = $tag->nums > 0 ? $tag->nums : 0;
- $tag->save();
- self::where(['aid'=>$aid, 'tid'=>$tag->id])->delete();
- }
- }
- }
- }
|