symfony-2.1 – Symfony2.1 – 使用DataTransformer时,选项“em”不存在

我正在使用 this食谱配方在Symfon 2.1中添加数据转换器,但是我收到以下错误,选项“em”不存在.已知的选项是:“attr”,“block_name”,….

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

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

解决方法

虽然我不能评论,如果这是最好的方式,我总是将它们作为一个硬依赖传递给我的任务构造函数

服务

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

调节器

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

表格类型

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;
    }

    // ...
}

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

相关文章

Css3如何实现鼠标移上变长特效?(图文+视频)
css3怎么实现鼠标悬停图片时缓慢变大效果?(图文+视频)
jquery如何实现点击网页回到顶部效果?(图文+视频)
css3边框阴影效果怎么做?(图文+视频)
css怎么实现圆角边框和圆形效果?(图文+视频教程)
Css3如何实现旋转移动动画特效