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"; } }