可捕获的致命错误:传递给Controller :: __ construct()的参数1必须是Doctrine \ ORM \ EntityManager的实例,没有给出,称为

时间:2016-02-04 06:19:01

标签: php symfony doctrine-orm twig entitymanager

通知控制器

<?php
namespace Main\AdminBundle\Controller;
/* included related namespace */
use Symfony\Component\PropertyAccess\PropertyAccess;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Query;

use Main\AdminBundle\Entity\Notificationmaster;

class NotificationController extends BaseController
  {
      protected $session;
       protected  $em;

public function __construct(EntityManager $em)
{
    $this->em = $em;
}

/**
 * @Route("/Notification1/{salon_id}",defaults={"salon_id":""})
 * @Template()
 */
public function indexAction($salon_id)
{
        return array("j"=>"jj");
}

/**
 * @Route("/Notification/create/{notification_type}/{notification_title}",defaults={"notification_type":"","notification_title":""})
 */
public function notificationcreateAction($notification_type,$notification_title)
{
    //$this->em = $em;

        $notificationmaster = new Notificationmaster();     
        $notificationmaster->setNotification_type($notification_type);
        $notificationmaster->setNotification_title($notification_title);
        $em = $this->getDoctrine()->getManager();
        $em->persist($notificationmaster);
        $em->flush();           
        return $notificationmaster->getNotification_master_id();    
}   
/**
 * @Route("/Notification/List/{notification_for}/{notification_to}/{lang_id}",defaults={"notification_for":"","notification_to":"","lang_id":""})
 */
 public function notificationlistAction($notification_for,$notification_to,$lang_id)
 {
        //$em = $this->getDoctrine()->getManager();
        return new Response(json_encode("hi")); 

 }
}

在twig文件中

 {% set notification_html = render(controller('MainAdminBundle:Notification:notificationlist',{"notification_for":"organization","notification_to":"1","lang_id":"1"})) %}

基本控制器

class BaseController extends Controller{

public function __construct()
{
    date_default_timezone_set("Asia/Calcutta");
 }
}

我在使用Twig file( as above )

调用通知列表操作时遇到此错误
  

捕获致命错误:参数1传递给Controller :: __ construct()   必须是Doctrine \ ORM \ EntityManager的实例,没有给出,称为

如果我删除实体管理器,那么我在创建操作中遇到错误,如

  

错误:在非对象上调用成员函数has()

因为我使用此

调用此创建操作
$notification = new NotificationController($em);
$notification_id = $notification->notificationcreateAction('appointment_book','New  Appointment');

所以我必须添加实体管理器。

1 个答案:

答案 0 :(得分:2)

首先,解决这个特殊的错误案例。如果你的问题包含整个NotificationController,似乎没有必要定义一个构造函数也没有让元素管理器存储在类变量中 - 你的notificationcreateAction从doctrine获取实体管理器实例并且没有'使用类变量。即只需完全删除构造函数和类变量:

class NotificationController extends BaseController
{
    protected $session;

    /**
     * @Route("/Notification1/{salon_id}",defaults={"salon_id":""})
     * @Template()
     */
    public function indexAction($salon_id)
    {
        return array("j"=>"jj");
    }

    // and so forth...

并将您在控制器上使用的代码更新为:

$notification = new NotificationController();
$notification_id = $notification->notificationcreateAction('appointment_book','New  Appointment');

更一般地回答这个问题

您不应该在不同的控制器中实例化控制器。相反,您应该为您可以从所有控制器调用的公共代码创建服务。有关symfony服务的更多信息:Service Container