Symfony,订阅者postPersist两次提交我的活动

时间:2019-02-16 16:03:12

标签: symfony subscriber

我有一个订阅者,当用户发表评论时,该订阅者会增加+1。 我用的是ChoiceType:

Positive => true,

中性=>空,

否定=>假

这是我的订户

public function preUpdate(PreUpdateEventArgs $event)
{
    //if positive == TRUE +1 total_feed_count +1 positive_count
    if ($event->getEntity() instanceof Feedback){
        if ($event->hasChangedField('positive') && $event->getNewValue('positive') == true){
            $event
                ->getEntityManager()
                ->getRepository(User::class)
                ->incrementCount($event->getNewValue('positive_count'))
                ->incrementCount($event->getNewValue('total_feed_count'))
            ;
        }
    }

    //if positive == FALSE +1 total_feed_count +1 negative_count
    if ($event->getEntity() instanceof Feedback) {
        if ($event->hasChangedField('positive') && $event->getNewValue('positive') == false) {
            $event
                ->getEntityManager()
                ->getRepository(User::class)
                ->incrementCountIfNegative($event->getNewValue('negative_count'))
                ->incrementCountIfNegative($event->getNewValue('total_feed_count'))
            ;
        }
    }
}

public function postPersist(LifecycleEventArgs $event)
{
    if ($event->getEntity() instanceof Feedback){
        $event
            ->getEntityManager()
            ->getRepository(User::class)
            ->incrementCount($event->getEntity()->getUser())
            ->incrementCountIfNegative($event->getEntity()->getUser())
        ;
    }
}

例如,如果我留下负面评论

doctrine.DEBUG:更新用户SET positive_count = positive_count + 1,total_feed_count = total_feed_count + 1 WHERE id =吗? [2] []

doctrine.DEBUG:更新用户SET negative_count = negative_count + 1,total_feed_count = total_feed_count + 1 WHERE id =吗? [2] []

doctrine.DEBUG:“提交” [] []

0 个答案:

没有答案