我正在尝试为帖子添加页面视图计数器,这是用户查看次数最多的.我在Post实体中添加了一个属性$ viewCount,它是一个整数.
我希望每次用户点击特定帖子的节目页面时都会对此进行计数.
要逐步完成整个过程,我需要设置一个计数器,每次查看时添加+1,将其存储在数据库中,然后查询,然后将其传回给Twig.
搜索hrs后我不知道怎么做的两部分是:
1)每次用户查看页面时如何添加(我知道你想以某种方式使用+1)
2)如何查询大多数页面视图以传递给控制器和树枝
的showAction
/**
* Show Post
*
* @param $slug
* @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
* @return array
*
* @Route("/post/{slug}", name="acme_demo_show")
* @Template("AcmeDemoBundle:Page:show.html.twig")
*/
public function showPostAction($slug)
{
$article = $this->getDoctrine()->getRepository('AcmeBundle:Post')
->findOneBy(array(
'slug' => $slug
));
if (null === $article) {
throw $this->createNotFoundException('Post was not found');
}
// Increment views each time user clicks on an article
$em = $this->getDoctrine()->getManager();
$views = $article->getViews();
$article->setViews($views + 1);
$em->flush();
return array(
'article' => $article,
);
}
Run Code Online (Sandbox Code Playgroud)
侧边栏动作 …