【问题标题】:Symfony2 - Page view counterSymfony2 - 页面浏览计数器
【发布时间】:2014-08-12 11:21:17
【问题描述】:

我正在尝试为用户查看次数最多的帖子添加页面查看计数器。我在我的 Post 实体中添加了一个属性 $viewCount,它是一个整数。

我希望每次用户点击特定帖子的显示页面时都会计算此值。

要逐步完成该过程,我需要设置一个计数器,每次查看时添加一个 +1,将其存储在数据库中,查询它然后将其传回给 Twig。

搜索了hrs后不知道怎么做的2个部分是:

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,
    );
}

侧边栏操作

public function sidebarAction()
{
    $em = $this->getDoctrine()->getManager();

    $post = $em->getRepository('AcmeDemoBundle:Article')
        ->getMostRecentArticles(5);

    if (!$post) {
        throw $this->createNotFoundException('No posts were found');
    }

    $articles = $this->getDoctrine()->getRepository('AcmeDemoBundle:Article')
        ->findBy(array(
            array(
                'views' => 'ASC'
            )
        ));

    return array(
        'post' => $post,
        'articles' => $articles
    );
}

树枝

<h3>Most Popular Articles</h3>
    {% for article in articles %}
        <a href="{{ path('acme_demo_article_show', { slug: article.slug }) }}" class="anchor" style="text-decoration: none">{{ article.title }}</a><br>
    {% endfor %}

【问题讨论】:

    标签: symfony doctrine-orm count counter pageviews


    【解决方案1】:

    如果您想在用户单击链接时增加计数器,您需要带有 AJAX 的 javascript。或者,您可以在帖子的控制器中使用纯 php 执行此操作,例如:

    $views = $article->getViews();
    $article->setViews($views + 1);
    $em->persist($article);
    $em->flush();
    

    最佳实践是将increment() 方法添加到您的Article 实体。

    然后,按浏览量查询文章:

     $articles = $this->getDoctrine()->getRepository('AcmeBundle:Post')
            ->findBy(array(), array('views' => 'ASC'));
    

    但更好的做法是在实体的存储库中编写您自己的方法。

    更新:

    存储库

    public function getMostPopularArticles()
    {
        return $this->createQueryBuilder('article')
            ->select('article')
            ->orderBy('article.views', 'DESC')
            ->getQuery()
            ->execute();
    
    }
    

    【讨论】:

    • 他还需要强制执行表/行锁定,否则他无法保证增量的正确性
    • @jperovic 你如何强制执行表/行锁定?
    • 由于您使用的是学说,因此有一篇关于此的文章:docs.doctrine-project.org/en/2.0.x/reference/…
    • 您的错误来自 findBy 方法。使用与我相同的语法。第一个数组我们可以留空,因为我们不是在寻找特定的字段值,第二个数组是 orderby 子句。 @jperovic 是完全正确的。
    • 上面评论中的文档链接现在失败了,对于其他到达这里的人来说:doctrine-orm.readthedocs.org/en/latest/reference/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-24
    • 1970-01-01
    • 2018-01-08
    • 1970-01-01
    相关资源
    最近更新 更多