【问题标题】: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 的实例,没有给出,调用
【发布时间】:2016-02-04 06:19:00
【问题描述】:

通知控制器

<?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 ) 调用通知列表操作时遇到此错误

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

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

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

因为我用这个来调用这个创建动作

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

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

【问题讨论】:

  • 你的BaseController是什么样的?
  • 类 BaseController 扩展控制器{ public function __construct() { date_default_timezone_set("Asia/Calcutta"); } }
  • 查看我对基本控制器的编辑
  • 您在哪里称呼您所指的这个创建操作?如果您从构造函数中删除 entityManager,则会给您一个错误...实例化这样的控制器似乎有点奇怪,但是如果您从该实例中删除 $em构造函数?
  • OR 如果您无法修改该实例化,您可以通过将控制器定义为服务并将 entitymanager 注入服务参数中来使 twig 调用正常工作...

标签: php symfony doctrine-orm twig entitymanager


【解决方案1】:

首先,解决这个特殊的错误情况。如果您的问题包含整个 NotificationController,则似乎不需要定义构造函数 nor 将元素管理器存储在类变量中 - 您的 notificationcreateAction 从教义中获取实体管理器实例并且没有t 使用类变量。 IE。只需完全删除构造函数和类变量:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-14
    • 1970-01-01
    • 1970-01-01
    • 2017-02-21
    • 1970-01-01
    • 2014-01-29
    相关资源
    最近更新 更多