我正在使用
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的复杂表单配置来为我做.