简体   繁体   English

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

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

Notification controller 通知控制器

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

 }
}

in twig file 在树枝文件中

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

Base Controller 基本控制器

class BaseController extends Controller{

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

i got this error while im calling notification list action using Twig file( as above ) 我在使用Twig file( as above )调用通知列表操作时收到了此错误Twig file( as above )

Catchable Fatal Error: Argument 1 passed to Controller::__construct() must be an instance of Doctrine\\ORM\\EntityManager, none given, called 可捕获的致命错误:传递给Controller :: __ construct()的参数1必须是Doctrine \\ ORM \\ EntityManager的实例,未给出任何实例,称为

if i remove entity manager , then i got an error in create action Like 如果我删除实体管理器,那么我在创建动作时会出错,例如

Error: Call to a member function has() on a non-object 错误:在非对象上调用成员函数has()

because i call this create action using this 因为我用这个叫这个创建动作

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

so i have to add entity manager . 所以我必须添加实体管理器。

First of, to address this particular error case. 首先,要解决这种特殊的错误情况。 If you question contains the whole NotificationController, there doesn't seem to be any need to define a constructor nor having the element manager stored in class variable - your notificationcreateAction fetches the entity manager instance from doctrine and doesn't use the class variable. 如果您的问题包含整个NotificationController,则似乎不需要定义构造函数, 无需将元素管理器存储在类变量中-您的notificationcreateAction从学说中获取实体管理器实例,并且不使用类变量。 Ie just remove the constructor and class variable completely: 即只是完全删除构造函数和类变量:

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...

And update your the code you use on your controller to: 并将您在控制器上使用的代码更新为:

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

To answer the question more generically 更笼统地回答这个问题

You shouldn't be instantiating controllers in different controllers. 您不应该在不同的控制器中实例化控制器。 Instead you should create a service for your common code that you can call from all of your controllers. 相反,您应该为可以从所有控制器调用的通用代码创建服务。 More about symfony services: Service Container 有关symfony服务的更多信息: Service Container

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 可捕获的致命错误:传递给AppBundle \\ Form \\ TagType :: __ construct()的参数1必须是Doctrine \\ ORM \\ EntityRepository的实例,未给出任何实例, - Catchable Fatal Error: Argument 1 passed to AppBundle\Form\TagType::__construct() must be an instance of Doctrine\ORM\EntityRepository, none given, 可捕获的致命错误:传递给 Album\Controller\AlbumController::__construct() 的参数 1 必须是 Album\Model\AlbumTable 的实例,没有给出 - Catchable fatal error: Argument 1 passed to Album\Controller\AlbumController::__construct() must be an instance of Album\Model\AlbumTable, none given 类型错误:参数1传递Chat :: __ construc t()必须是Doctrine \\ ORM \\ EntityManager的实例,未给出任何实例,在 - Type error: Argument 1 passed Chat::__construc t() must be an instance of Doctrine\ORM\EntityManager, none given, called in Symfony2:ContextErrorException:可捕获的致命错误:传递给[…] :: __ construct()的参数1必须实现接口[…]没有给出 - Symfony2: ContextErrorException: Catchable Fatal Error: Argument 1 passed to […]::__construct() must implement interface […] none given 切换表时出现zend db错误可捕获的致命错误:传递给__construct()的参数1必须是一个数组,给定对象,在 - zend db error on switching tables Catchable fatal error: Argument 1 passed to __construct() must be an array, object given, called in 主义(Symfony3)可捕获的致命错误:传递给(bundle)的参数1必须是(bundle)的实例,给定数组 - Doctrine (Symfony3) Catchable Fatal Error: Argument 1 passed to (bundle) must be an instance of (bundle), array given 可捕获的致命错误:传递给getPrice()的参数1必须是Rectangle的实例,没有给出 - Catchable fatal error: Argument 1 passed to getPrice() must be an instance of Rectangle, none given 可捕获的致命错误:传递给...的参数1必须是...,给定数组的实例 - Catchable Fatal Error: Argument 1 passed to … must be an instance of …, array given 可捕获的致命错误:传递给…的参数1必须是…的一个实例,为boolean给定,在…上在…中调用,并在…上在第在线 - Catchable fatal error: Argument 1 passed to … must be an instance of …, boolean given, called in … on line … and defined in … on line 可捕获的致命错误:传递给Doctrine \\ DBAL \\ Connection :: update()的参数3必须为数组类型,在symfony2 update中没有给出 - Catchable Fatal Error: Argument 3 passed to Doctrine\DBAL\Connection::update() must be of the type array, none given in symfony2 update Action
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM