我正在使用Symfony 2.1.3-DEV并尝试实现转换实体到字符串(某种ID),然后在表单提交时从字符串返回到实体。如果我使用的食谱给变压器的问题是一样的: http://symfony.com/doc/master/cookbook/form/data_transformers.htmlSymfony2表单实体到字符串转换器问题
控制器代码:
$task = $entityManager->find('AcmeTaskBundle:Task', $id);
$form = $this->createForm(new TaskType(), $task); // so $task->issue is Issue object
我得到这个错误:
The form's view data is expected to be an instance of class Acme\TaskBundle\Entity\Issue, but is a(n) string. You can avoid this error by setting the "data_class" option to null or by adding a view transformer that transforms a(n) string to an instance of Acme\TaskBundle\Entity\Issue.
的事情是,我已经有一个变换器,它转换为字符串。
从Form.php
:
if (null !== $dataClass && !$viewData instanceof $dataClass) {
throw new FormException(
//...
);
}
为什么$viewData
被检查为data_class
参数(或者猜到类型给定对象的)的实例?不是视图数据应该是字符串/数组等?我错过了什么吗?
您能向我们展示您的表单类型吗?帮助你会更容易! – Snroki