【发布时间】: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