src/PHPDish/Bundle/UserBundle/Service/UserManager.php
public function followUser(UserInterface $user, UserInterface $follower)
    {
        if ($user->getId() == $follower->getId()) {
            throw new \LogicException($this->translator->trans('follow.cannot_follow_yourself'));
        }
        $user->addFollower($follower);
        $user->setFollowerCount($user->getFollowerCount() + 1);
        $follower->setFollowingCount($follower->getFollowingCount() + 1);

        $this->objectManager->persist($user);
        $this->objectManager->persist($follower);
        $this->objectManager->flush();

        //触发事件
        $this->eventDispatcher->dispatch(Events::USER_FOLLOWED, new UserFollowedEvent($user, $follower));

        return true;
    }
public function followUser(UserInterface $user, UserInterface $follower)
    {
        if ($user->getId() == $follower->getId()) {
            throw new \LogicException($this->translator->trans('follow.cannot_follow_yourself'));
        }
        $user->addFollower($follower);
        $user->setFollowerCount($user->getFollowerCount() + 1);
        $follower->setFollowingCount($follower->getFollowingCount() + 1);

        $this->objectManager->persist($user);
        $this->objectManager->persist($follower);
        $this->objectManager->flush();

        //触发事件
        $this->eventDispatcher->dispatch(Events::USER_FOLLOWED, new UserFollowedEvent($user, $follower));

        return true;
    }
{@inheritdoc}