Catchable Fatal Error: Argument 1 passed to Controller::__construct() must be an instance of Doctrine\ORM\EntityManager, none given, called

Catchable Fatal Error: Argument 1 passed to Controller::__construct() must be an instance of Doctrine\ORM\EntityManager, none given, called

通知控制器

<?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")); 

 }
}

在树枝文件中

 {% 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 )

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

Catchable Fatal Error: Argument 1 passed to Controller::__construct() must be an instance of Doctrine\ORM\EntityManager, none given, called

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

Error: Call to a member function has() on a non-object

因为我用这个

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

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

首先,解决这个特殊的错误案例。如果您的问题包含整个 NotificationController,似乎没有必要定义构造函数 将元素管理器存储在 class 变量中 - 您的 notificationcreateAction 获取实体manager 实例来自 doctrine,不使用 class 变量。 IE。完全删除构造函数和 class 变量:

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