protected function handle() { $request = $this->getRequest(); if ($request->getMethod() == 'POST') { $posts = $request->request; try { $name = $posts->get('name'); if (!$name) { throw new \Exception('标签名不能为空'); } $description = $posts->get('description'); $slug = $posts->get('slug'); if (!$slug) { $slug = null; } // 判断slug是否重复 if ($slug) { $tag = TagModel::getTagBySlug($slug); if ($tag) { throw new \Exception('标签URL占位符已被使用'); } } // 创建tag $tag = new TagModel(); $tag->name = $name; $tag->description = $description; $tag->slug = $slug; $now = time(); $tag->createTimestamp = $now; $tag->updateTimestamp = $now; // 保存 $tag = TagModel::createTag($tag); return new JsonResponse(array('status' => 1, 'data' => array('tag' => $tag->toArray()))); } catch (\Exception $e) { return new JsonResponse(array('status' => 0, 'data' => $e->getMessage())); } } return $this->render('tag/add.html.twig'); }
public function __invoke($id_or_slug = null) { $request = $this->getRequest(); $page = $request->query->get('page'); $size = 15; // 优先按照slug来查询 $tag = TagModel::getTagBySlug($id_or_slug); if (!$tag) { $tag = TagModel::getTag($id_or_slug); } // 查找出tag下的文章 $article_tags = ArticleTagModel::allRelationship(function (QueryBuilder $qb) use($tag) { $qb->andWhere($qb->expr()->eq('tag_id', ':tag_id'))->setParameter(':tag_id', $tag->id); }); $article_ids = array(); foreach ($article_tags as $article_tag) { $article_ids[] = $article_tag['article_id']; } $articles_pager = ArticleModel::listArticles($page, $size, function (QueryBuilder $qb) use($article_ids) { $qb->andWhere($qb->expr()->gt('publish_timestamp', 0)); if ($article_ids) { $qb->andWhere($qb->expr()->in('id', $article_ids)); } else { $qb->andWhere($qb->expr()->eq('id', 0)); } $qb->addOrderBy('publish_timestamp', 'desc'); }); $articles = $articles_pager->getData(); foreach ($articles as $key => $article) { // 取出文章的图片媒体 $article['media'] = MediaModel::allMedias(function (QueryBuilder $qb) use($article) { $qb->andWhere($qb->expr()->eq('article_id', ':article_id'))->setParameter(':article_id', $article['id']); $qb->andWhere($qb->expr()->eq('type', ':type'))->setParameter(':type', 'picture'); }); // 取出文章的tag标签 $article_tags = ArticleTagModel::allRelationship(function (QueryBuilder $qb) use($article) { $qb->andWhere($qb->expr()->eq('article_id', ':article_id'))->setParameter(':article_id', $article['id']); }); $article['tags'] = array(); foreach ($article_tags as $article_tag) { $t = TagModel::getTag($article_tag['tag_id']); $article['tags'][] = $t->toArray(); } // 发布者 $article['publisher'] = array(); $article_publishers = ArticlePublisherModel::allRelationship(function (QueryBuilder $qb) use($article) { $qb->where($qb->expr()->eq('article_id', ':article_id'))->setParameter(':article_id', $article['id']); }); if ($article_publishers) { $article_publisher = array_shift($article_publishers); $publisher_id = $article_publisher['publisher_id']; $publisher = PublisherModel::getPublisher($publisher_id); if ($publisher) { $article['publisher'] = $publisher; } } $articles[$key] = $article; } $articles_pager->setData($articles); return $this->render('tag/blogs.html.twig', array('blog_posts' => $articles_pager, 'tag' => $tag)); }