我正在使用symfony控制器作为服务。但是当我在控制器中调用doctrine manager时,它会给出错误FatalErrorException:错误:在非对象上调用成员函数has()。
这是我的控制器:
namespace Acme\StoreBundle\Controller; use Doctrine\ORM\EntityManager; class ServiceController extends Controller { /** * * @var EntityManager */ protected $em; public function __construct(EntityManager $em) { $this->em = $em; } }
和我的services.yml就像:
服务:
service_controller:
class: Acme\StoreBundle\Controller\ServiceController
arguments: ["@doctrine.orm.entity_manager"]
我在DbController:
的另一个控制器中调用实体管理器<?php
public function users()
{
$query = $this->em->createQuery('select u from AcmeStoreBundle:User u');
$user = $query->getResult();
}
答案 0 :(得分:1)
您可以在ServiceController中获取EntityManager,如:
$em = $this->getDoctrine()->getManager();
所以以下代码没有意义:
service_controller:
class: Acme\StoreBundle\Controller\ServiceController
arguments: ["@doctrine.orm.entity_manager"]
我真的想看看你的DbController代码,你能表明吗?
我的DbController就像:
<?php
namespace Acme\StoreBundle\Controller;
use Acme\StoreBundle\Entity\UserCategoryTag;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class DbController extends Controller
{
//Suppose you want get all users you can do some like this..
public function Users() {
$em = $this->getDoctrine()->getManager();
$repository = $em->getRepository('AcmeStoreBundle:User');
$user = $repository->findAll();
}
}
答案 1 :(得分:0)
您还可以使用per-execute方法,如下所示:
1]为预执行方法
创建新的侦听器类namespace Acme\Listener;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
/**
* Acme\Listener\ControllerListener
*/
class ControllerListener
{
/**
* On Core Controller this is used to set pre execute
*
* @param GetResponseEvent $event
*/
public function onCoreController(FilterControllerEvent $event)
{
if (HttpKernelInterface::MASTER_REQUEST === $event->getRequestType()) {
$_controller = $event->getController();
if (isset($_controller[0])) {
$controller = $_controller[0];
if (method_exists($controller, 'preExecute')) {
$controller->preExecute();
}
}
}
}
}
2]在配置文件app / config / config.yml
中配置监听器services:
controller.pre_execute_listener:
class: Acme\Listener\ControllerListener
tags:
- { name: kernel.event_listener, event: kernel.controller, method: onCoreController }
3]控制器文件
namespace Acme\StoreBundle\Controller;
use Doctrine\ORM\EntityManager;
class ServiceController extends Controller {
public function preExecute()
{
$this->em = $this->getDoctrine()->getEntityManager();
}
}
4]你的功能
<?php
public function users()
{
$query = $this->em->createQuery('select u from AcmeStoreBundle:User u');
$user = $query->getResult();
}
答案 2 :(得分:0)
我通过尝试很多东西解决了这个问题。 这是解决方案:
你必须使用你的控制器作为服务。我使用TagClass控制器作为服务。这是我的控制器代码:
<?php
namespace Acme\StoreBundle\Controller;
class TagClassController extends Controller
{
public $em;
public function __construct(Entity Manager $em)
{
$this->em = $em;
}
public function ParseWebsite($categoryid){
}
}
?>
要使用此功能,您必须在services.yml中将控制器定义为服务,如下所示:
parameters:
acme.controller.tagclass.class: Acme\StoreBundle\Controller\TagClassController
services:
acme.controller.tagclass:
class: "%acme.controller.tagclass.class%"
arguments: [ @doctrine.orm.entity_manager ]
你可以在TagClass中调用任何方法,如下所示:
$this->forward('acme.controller.tagclass:ParseWebsite',array($categoryid));