Symfony 3.4未定义属性:Doctrine \ ORM \ EntityManager :: $ flush

时间:2018-03-27 20:04:35

标签: symfony doctrine-orm entity

我试图创建一项服务但收效甚微。

class NotificationService {

public $manager;

public function __construct($manager) {

    $this->manager = $manager;
}

public function set($user, $type, $typeId, $extra = null){

    $em = $this->manager;

    $notification = new Notification();
    $notification->setUser($user);
    $notification->setType($type);
    $notification->setTypeId($typeId);
    $notification->setReaded(0);
    $notification->setCreatedAt(new \DateTime('now'));
    $notification->setExtra($extra);

    $em->persist($notification);
    $flush = $em->flush;

    if($flush == null){
        $status = true;
    }else{
        $status = false;
    }

    return $status;
}

}

这是我的服务。:

app.notification_service:
    class: AppBundle\Services\NotificationService
    public: true
    arguments: ['@doctrine.orm.entity_manager']

我在LikeController中调用该服务。这个想法是在有人喜欢他们的帖子时通知会员:

public function likeAction(Request $request){
    $user = $this->getUser();
    $id = $request->get('id');

    $em = $this->getDoctrine()->getManager();

    $publication = $em->getRepository('BackendBundle:Publication')->find($id);

    $like = new Like();
    $like->setUser($user);
    $like->setPublication($publication);

    $em->persist($like);
    $flush = $em->flush();

    if($flush == NULL){
        $notification = $this->get('app.notification_service');
        $notification->set($publication->getUser(), 'like', $user->getId(), $publication->getId());

        $status = 'You like this post';
    }else{
        $status = 'Could not save the like.';
    }

    return new JsonResponse(['status' => $status]);
}

我测试它时不起作用。相反,我得到一个例外,EntityManager无法识别flush方法:

未定义的属性:Doctrine \ ORM \ EntityManager :: $ flush

有人能告诉我如何解决这个问题吗?谢谢。

1 个答案:

答案 0 :(得分:1)

你必须写下这个:

$flush = $em->flush();

代替:

$flush = $em->flush;