【问题标题】:Count cache in Symfony2 with a listener用监听器计算 Symfony2 中的缓存
【发布时间】:2016-02-11 14:09:21
【问题描述】:

我在 Symfony2 中构建一个博客,它有两个实体:BlogBlogComment(这是一个简化的示例)。 BlogBlogComment 具有 OneToMany 关系。发布BlogComment 时,属性published 设置为false。管理员批准BlogComment后,设置为true

我想用我所有的Blog-posts 创建一个概览,并在两个单独的字段中显示published = truepublished = falseBlogComment 的数量。可以将所有 BlogComment 放在一个循环中并进行计数,但因为它可能是对 Blog-posts 的一个非常大的概述,所以这不是我的首选。

我在Blog 创建了两个属性:published_comments_countunpublished_comments_count。为了更新这些字段,我为BlogComment 创建了一个监听器:

class BlogCommentListener
{
    public function onFlush(OnFlushEventArgs $args)
    {
        $em = $args->getEntityManager();
        $uow = $em->getUnitOfWork();

        $entities = array_merge(
            $uow->getScheduledEntityInsertions(),
            $uow->getScheduledEntityUpdates(),
            $uow->getScheduledEntityDeletions()
        );

        foreach($entities as $entity){
            if($entity instanceof BlogComment){
                $Blog = $entity->getBlog();

                $published_comments_count = 0;
                $unpublished_comments_count = 0;

                foreach($Blog->getComments() as $BlogComment){
                    if($BlogComment->getPublished()){
                        $published_comments_count++;
                    } else {
                        $unpublished_comments_count++;
                    }
                }

                $Blog->setPublishedCommentsCount($published_comments_count);
                $Blog->setUnpublishedCommentsCount($unpublished_comments_count);

                $em->persist($Blog);

                $md = $em->getClassMetadata(get_class($Blog));
                $uow->recomputeSingleEntityChangeSet($md, $Blog);
            }
        }
    }
}

它工作得很好,但是当我添加新评论时,它还没有在$Blog->getComments() 的 ArrayCollection 中。有没有办法计算这个 ArrayCollection 的变化?

【问题讨论】:

    标签: symfony caching listener counter


    【解决方案1】:

    我认为不可能计算 arrayCollection 中的更改,但您可以通过为每个实体添加或删除 1 来更改逻辑:

    class BlogCommentListener
    {
        public function onFlush(OnFlushEventArgs $args)
        {
            $em = $args->getEntityManager();
            $uow = $em->getUnitOfWork();
    
            $insertions = $uow->getScheduledEntityInsertions();
            $deletions = $uow->getScheduledEntityDeletions();
    
            foreach($insertions as $entity){
                if($entity instanceof BlogComment){
                    $Blog = $entity->getBlog();
    
                    $Blog->setUnpublishedCommentsCount($Blog->getUnpublishedCommentsCount()+1);//Here the comment inserted must be unpublished
    
                    $em->persist($Blog);
    
                    $md = $em->getClassMetadata(get_class($Blog));
                    $uow->recomputeSingleEntityChangeSet($md, $Blog);
                }
            }
        }
        foreach($deletions as $entity){
            if($entity instanceof BlogComment) {
                $Blog = $entity->getBlog();
    
                if ($entity->getPublished()) {
    
                    $Blog->setPublishedCommentsCount($Blog->getPublishedCommentsCount()-1);//Here the comment inserted must be unpublished
                } else {
                     $Blog->setUnpublishedCommentsCount($Blog->getUnpublishedCommentsCount()-1);
                }                
                $em->persist($Blog);
    
                $md = $em->getClassMetadata(get_class($Blog));
                $uow->recomputeSingleEntityChangeSet($md, $Blog);
            }
        }
    }
    

    另外,我会为您提供一个更简单的解决方案:在您的树枝模板中使用计数过滤器:

    {% for blog in blogs %}
        <li><h4>blog.title</h4>
        <p>comments published : {{ blog.comments.published|count }}</p>
        <p>comments non-published : {{ (not blog.comments.published)|count }}</p>
    {% endfor %}
    

    【讨论】:

    • 谢谢,您提出的第一个解决方案可能是最好的方法。在其他一些更复杂的情况下,我需要相同的设置。第二个解决方案不起作用,据我所见,twig 中没有像这样工作的计数过滤器。我尽量避免为了计数而循环遍历所有 cmets,这就是为什么我喜欢第一个解决方案:)
    • 哎呀抱歉,树枝过滤器是长度而不是计数。 :) 但无论如何,我认为由于您发布的属性(Twig 无法计算布尔值),它不起作用。但是,如果你想停止遍历实体来计算它们,我认为最好的方法是使用你的博客存储库。我会编辑我的答案
    猜你喜欢
    • 2012-10-25
    • 2012-05-25
    • 1970-01-01
    • 2012-12-13
    • 1970-01-01
    • 2020-06-19
    • 1970-01-01
    • 2011-12-31
    • 1970-01-01
    相关资源
    最近更新 更多