Symfony2.1 - 选项“em"使用 DataTransformer 时不存在 [英] Symfony2.1 - The option "em" does not exist when using DataTransformer

查看:32
本文介绍了Symfony2.1 - 选项“em"使用 DataTransformer 时不存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 this 食谱配方在Symfon 2.1,但我收到以下错误,选项em"不存在.已知选项有:attr"、block_name"、....

这仍然是将实体管理器发送到表单类型的有效方法吗?

$taskForm = $this->createForm(new TaskType(), $task, array('他们' =>$this->getDoctrine()->getEntityManager(),));

解决方案

虽然我无法评论这是否是最好的方法,但我总是将它们作为硬依赖项传递给我的任务构造函数...

服务

服务:my_bundle.form.type.task:类:公司\MyBundle\Form\Type\TaskType论据:- @doctrine.orm.entity_manager

控制器

$form = $this->createForm($this->get('my_bundle.form.type.task'), $task);//或者$form = $this->createForm(new TaskType($this->getDoctrine()->getEntityManager()));

表单类型

命名空间 Company\MyBundle\Form\Type;使用 Doctrine\ORM\EntityManager;使用 Symfony\Component\Form\AbstractType;//...类 TaskType 扩展 AbstractType{受保护的 $em;公共函数 __construct(EntityManager $em){$this->em = $em;}//...}

只要我的表单类型有任何依赖项,我就会使用容器来管理它们.我个人觉得这个方法比依赖 Symfony 复杂的表单配置来为我做这件事更清楚发生了什么,以及我的自定义类需要什么.

I am using this cookbook recipe to add a data transformer in Symfon 2.1, but I am getting the following error, The option "em" does not exist. Known options are: "attr", "block_name",....

Is this still a valid way to send the entity manager over to the form type?

$taskForm = $this->createForm(new TaskType(), $task, array(
    'em' => $this->getDoctrine()->getEntityManager(),
));

解决方案

While I can't comment if that's the best way or not, I've always passed them to my task constructor as a hard dependency...

Services

services:
    my_bundle.form.type.task:
        class: Company\MyBundle\Form\Type\TaskType
        arguments:
            - @doctrine.orm.entity_manager

Controller

$form = $this->createForm($this->get('my_bundle.form.type.task'), $task);
// or
$form = $this->createForm(new TaskType($this->getDoctrine()->getEntityManager()));

Form Type

namespace Company\MyBundle\Form\Type;

use Doctrine\ORM\EntityManager;
use Symfony\Component\Form\AbstractType;
// ...

class TaskType extends AbstractType
{
    protected $em;

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

    // ...
}

As soon as my form types have any dependencies, I use the container to manage them. I personally find this method much more clear of what's going on, and what my custom classes require than relying on Symfony's complex form configuration to do it for me.

这篇关于Symfony2.1 - 选项“em"使用 DataTransformer 时不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆