【问题标题】:Symfony2 injecting entityManager errorSymfony2注入entityManager错误
【发布时间】:2016-01-19 14:53:34
【问题描述】:

我正在关注 Doctrine ORM Symfony2 Documentation。当谈到将对象持久化到数据库时,我收到此错误:

Attempted to call an undefined method named "getDoctrine" of class "BooksApi\BookBundle\Controller\IndexController".

我在代码中唯一不同的是我正在尝试将 EntityManager 创建为服务....

services.xml:

<?xml version="1.0" encoding="UTF-8"?>
<container xmlns="http://symfony.com/schema/dic/services"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
    <services>
        <service id="booksapi.controller.index"
                 class="BooksApi\BookBundle\Controller\IndexController">
            <argument type="service" id="booksapi.repositories.test_repository" />
            <argument type="service" id="doctrine.orm.entity_manager" />
        </service>
    </services>
</container> 

我的索引控制器:

<?php

namespace BooksApi\BookBundle\Controller;

use BooksApi\BookBundle\Entity\BooksEntity;
use Doctrine\ORM\EntityManager;
use Symfony\Component\HttpFoundation\Response;


class IndexController
{
    /**
     * @var EntityManager
     */
    public $em;

    /**
     *  @param EntityManager $entityManager
     */
    public function __construct(
        EntityManager $entityManager
    ){
        $this->em = $entityManager;
    }

    /**
     * @return Response
     */
    public function testAction()
    {

        $book = new BooksEntity();
        $book->setTitle('Tomazi in da Jungle');
        $book->setPrice('19.99');
        $book->setDescription('Lorem ipsum dolor');

        $this->em = $this->getDoctrine()->getManager();

        $this->em->persist($book);
        $this->em->flush();

        return new Response('Created product id '.$book->getId());
    }
}

所以查看错误 getDoctrine method is not recognized....知道为什么...? 我该如何解决这个问题。

【问题讨论】:

  • 你错过了显而易见的事情。 $this->em 在你的构造函数中设置,所以删除 $this->em = $this->getDoctrine()->getManager();一切都会好起来的。

标签: php symfony doctrine-orm


【解决方案1】:

1/ 快速解决方案:

删除此行:$this-&gt;em = $this-&gt;getDoctrine()-&gt;getManager();

2/ 更好的解决方案:

IndexController 应该扩展 Controller

(Symfony\Bundle\FrameworkBundle\Controller\Controller)

并且可以使用getDoctrine 方法。这样,Doctrine Entity Manager 就不需要注入了。没有构造函数,没有服务定义。

【讨论】:

  • 除了扩展控制器之外还有什么办法...?
  • ---> 删除此行$this-&gt;em = $this-&gt;getDoctrine()-&gt;getManager();
  • 是的,就像你做的那样:注入 Doctrine 实体管理器。所以,使用我建议的第一个选项。
【解决方案2】:

定义控制器是一个很好的做法,所以我会坚持下去。这里有两件事似乎不对:

  1. 您的服务定义 (services.xml) 包含两个参数,而您的 Controller 构造函数只接受一个参数。

  2. 这一行:$this-&gt;em = $this-&gt;getDoctrine()-&gt;getManager();:你根本不需要它,因为你的$this-&gt;em 已经在构造函数中定义了,它的值是一个EntityManager 实例。只需删除此行,您应该会很好

你得到这个错误的原因仅仅是因为你试图使用getDoctrine 方法,这是一个Controller 方法。它does 只是要求Container 创建EntityManager 的实例,并且由于您已经将该实例注入constructor,因此根本不需要此调用(getDoctrine

【讨论】:

  • 完美这个作品。谢谢你的回答。不幸的是,Scoolnico 先回答了,所以会接受他的回答,但也投了你的票。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-04-12
  • 2011-05-07
  • 1970-01-01
  • 1970-01-01
  • 2011-06-10
  • 2017-05-19
  • 1970-01-01
相关资源
最近更新 更多