1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- <?php
- declare (strict_types = 1);
- namespace app\command;
- // 引入框架内置类
- use think\console\Input;
- use think\console\Output;
- use think\console\input\Argument;
- use think\console\Command;
- use think\facade\Db;
- use app\model\Tag;
- use app\model\Article;
- use app\model\TagArticle;
- class TagArticleConsole extends Command
- {
- protected function configure()
- {
- $this->setName('tagindex')
- ->addArgument('name', Argument::OPTIONAL, "console name")
- ->setDescription('Create a new tag index');
- }
- protected function execute(Input $input, Output $output)
- {
- $name = trim($input->getArgument('name'));
- $name = $name ?: 'thinkphp';
- $this->$name();
- }
- public function thinkphp()
- {
- echo "hello thinkphp";
- }
- public function makeTagArticleIndex()
- {
- // 查找所有keywords
- $keywordsList = Db::table("hu_article")->field('id,cid,keywords')->select();
- $i = $j = 0;
- foreach ($keywordsList as $value) {
- $id = $value['id'];
- $cid = $value['cid'];
- $keywords = $value['keywords'];
- $tags = explode(",",$keywords);
- foreach ($tags as $val) {
- $tagname = trim($val);
- if ($tagname) {
- $tag = Tag::where('tagname', $tagname)->findOrEmpty();
- if ($tag->isEmpty()) {
- $tag->tagname = $tagname;
- $tag->nums = 1;
- $tag->save();
- TagArticle::create([
- 'tid' => $tag->id,
- 'aid' => $id,
- 'cid' => $cid
- ]);
- $i++;
- $j++;
- } else {
- $tagArticle = TagArticle::where('aid', $id)->where('tid', $tag->id)->findOrEmpty();
- if ($tagArticle->isEmpty()) {
- $tagArticle->aid = $id;
- $tagArticle->cid = $cid;
- $tagArticle->tid = $tag->id;
- $tagArticle->save();
-
- $tag->nums += 1;
- $tag->save();
- $j++;
- }
- }
- }
- }
- }
- echo "新增标签:" . $i . "\n" . "新增标签关联文章" . $j . "\n";
- }
- }
|