public function __invoke() { // 将所有文章用博客的形式展现 $structure = StructureModel::search('home'); // 筛选出所有发布的文章 $request = $this->getRequest(); $page = $request->query->get('page'); $size = 15; $articles_pager = ArticleModel::listArticles($page, $size, function (QueryBuilder $qb) use($structure) { $qb->andWhere($qb->expr()->gt('publish_timestamp', 0)); if ($structure) { $qb->andWhere($qb->expr()->eq('structure_id', ':structure_id'))->setParameter(':structure_id', $structure->id); } $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; } } // 评论 $article['comments'] = CommentModel::getCount(function (QueryBuilder $qb) use($article) { $qb->where($qb->expr()->eq('article_id', ':article_id'))->setParameter(':article_id', $article['id']); }); $articles[$key] = $article; } $articles_pager->setData($articles); return $this->render('homepage/blogs.html.twig', array('blog_posts' => $articles_pager)); }