>通知控制器
<?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 传递给控制器::__construct() 必须是 Doctrine''ORM''EntityManager 的实例,没有给出,称为
如果我删除实体管理器,那么我在创建操作中出现错误 喜欢
错误:对非对象上的成员函数 has() 的调用
因为我用这个调用这个创建操作
$notification = new NotificationController($em);
$notification_id = $notification->notificationcreateAction('appointment_book','New Appointment');
所以我必须添加实体管理器.
首先,解决这个特定的错误情况。如果您的问题包含整个 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服务:服务容器