Symfony 3.4未定义属性:条令\ORM\EntityManager::$flush
我正在尝试创建一个几乎没有成功的服务Symfony 3.4未定义属性:条令\ORM\EntityManager::$flush,symfony,doctrine-orm,entity,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; $notificatio
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;
}
}
这是我的服务。yml:
app.notification_service:
class: AppBundle\Services\NotificationService
public: true
arguments: ['@doctrine.orm.entity_manager']
我打电话给LikeControl的服务。这样做的目的是在有人喜欢他们的帖子时通知会员:
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方法:
未定义的属性:条令\ORM\EntityManager::$flush
有人能告诉我怎么解决这个问题吗?谢谢。您必须写下:
$flush=$em->flush()代码>
相反:
$flush=$em->flush代码>$em->flush();帕伦夫妇很重要,我已经想了4个小时了。我觉得自己像个白痴!谢谢你的帮助。