【问题标题】:Calling Doctrine from custom class从自定义类调用 Doctrine
【发布时间】:2012-12-23 13:13:21
【问题描述】:

/已编辑/

我有这门课:

namespace Baza\BlogBundle\Form;   
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Doctrine\ORM\EntityManager; 

class filterType extends AbstractType
{

  protected $em;
  public function __construct(EntityManager $em)
 {
     $this->em = $em;
 }
public function buildForm(FormBuilderInterface $builder, array $options)
{

   $this->$em->getDoctrine()->getEntityManager();

   /****
   ****/

 }
}

这是我的服务 yml:

services:
 filterType:
    class: Baza\BlogBundle\Form\filterType
    arguments: [doctrine.orm.entity_manager]

当我运行代码时,出现以下异常:

可捕获的致命错误:传递给 Baza\BlogBu​​ndle\Form\filterType::__construct() 的参数 1 必须是 Doctrine\ORM\EntityManager 的实例,没有给出

我完全没有想法。

【问题讨论】:

  • 试试:arguments: [@doctrine.orm.entity_manager] 添加了@,不知道是什么意思,但我所有的 Symfony 代码都使用它。
  • 我试过了,我得到:“扫描下一个令牌时出现 ScannerException,我们在编辑器中发现了这个无法启动任何令牌的字符 @(64)”。
  • 确保您的空格数正确。
  • 为什么要添加 Form 类型为 Doctrine prePersist
  • 我正在尝试一些东西,我很确定这不会导致这里出现错误。

标签: php symfony doctrine doctrine-orm


【解决方案1】:

我自己创建了 FormType。这应该有效:

<?php
// Baza\BlogBundle\Form\filterType.php

namespace Baza\BlogBundle\Form;  

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Doctrine\ORM\EntityManager;

class filterType extends AbstractType
{
  protected $em;

  public function __construct(EntityManager $em)
  {
     $this->em = $em;
  }

  public function buildForm(FormBuilderInterface $builder, array $options)
  {
    // Do something with your Entity Manager using "$this->em"
  }

  public function getName()
  {
      return 'filter_type';
  }
}

在你的控制器中使用类似的东西

<?php
// Baza\BlogBundle\Controller\PageController.php

namespace Baza\BlogBundle\Controller;
use Baza\BlogBundle\Form\filterType;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class BaseController extends Controller
{
     public function testEntityManager()
     {
         // assign whatever you need
         $enquiry = null;
         // getEntityManager() is depricated. Use getManager() instead.
         $em = $this->getDoctrine()->getManager();

         $this->createForm(
             new filterType($em),
             $enquiry
         );
     } 
}

永远不要忘记包含/使用您正在使用的所有类。否则 PHP 将假定该类在您当前使用的命名空间内。

这就是你得到错误的原因(在 Cerad 的帖子上)

Catchable Fatal Error: Argument 1 passed to
Baza\BlogBundle\Form\filterType::__construct()
must be an instance of Baza\BlogBundle\Form\EntityManager [...]

由于您没有包含 EntityManager,PHP 假定它是您当前命名空间中的一个类,即 Baza\BlogBundle\Form


看起来很有趣的 Class EntityManager50ecb6f979a07_546a8d27f194334ee012bfe64f629947b07e4919\__CG__\Doctrin‌​e\ORM\EntityManager 是一个 Doctrine2 代理类。

从 Symfony 2.1 开始,调用 $this-&gt;getDoctrine()-&gt;getEntityManager() 不再产生 Doctrine\ORM\EntityManager 而是一个代理类,它的行为实际上与原始 EntityManager 一样,并且可以毫无问题地传递。

【讨论】:

  • 在我的示例代码中,我使用了$this-&gt;em。您似乎不小心使用了$this-&gt;$emem 前面有一个 $),它查找名为$em 的变量并将其值用作$this 的属性名称。由于您没有定义$em,PHP 尝试访问$this-&gt;null(或多或少)。要访问对象的属性,请使用$object-&gt;property,因此在本例中为$this-&gt;em
【解决方案2】:

需要@ 符号来指示参数是服务。但是,正如您所发现的,@ 会使 yaml 解析器出错。解决方案是使用引号。

services:
    filterType:
        class: Baza\BlogBundle\Form\filterType
        arguments: ['@doctrine.orm.entity_manager']

我记得我也花了几个小时才弄明白。

【讨论】:

  • 感谢您的回复。错误已更改为:Argument 1 passed to ... filterType::__construct() must be an instance of Baza\BlogBundle\Form\EntityManager, instance of EntityManager50ec...919\__CG__\Doctrine\ORM\EntityManager given 我正在像这样在控制器中传递参数:$em = $this-&gt;getDoctrine()-&gt;getEntityManager(); $form = $this-&gt;createForm(new filterType($em), $enquiry); 我错过了什么?
  • 第一个错误是说我没有传递任何参数:none given,现在它说传递的参数是错误的:instance of EntityManager50ec...919\__CG__\Doctrine\ORM\EntityManager given。错误信息的第二部分不同。
  • 这是完整的错误信息:Catchable Fatal Error: Argument 1 passed to Baza\BlogBundle\Form\filterType::__construct() must be an instance of Baza\BlogBundle\Form\EntityManager, instance of EntityManager50ecb6f979a07_546a8d27f194334ee012bfe64f629947b07e4919\__CG__\Doctrine\ORM\EntityManager given, called in C:\xampp\htdocs\baza\src\Baza\BlogBundle\Controller\PageController.php on line 230 and defined in C:\xampp\htdocs\baza\src\Baza\BlogBundle\Form\filterType.php line 18
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-09-25
  • 2015-06-20
  • 1970-01-01
  • 1970-01-01
  • 2015-09-25
  • 2012-11-13
  • 2014-07-17
相关资源
最近更新 更多