Php Symfony2 formtype如何传递数据
我想构建一个表单,其中包含来自2个相关实体的数据,并且能够用一次提交更新这两个实体。到目前为止,我得到了一个工作表单,它使用formtype,但只使用静态数据(例如$foo='bar')。我不知道如何将数据从我的控制器传递到formtype并在我的formtype中使用它 这就是我得到的:Php Symfony2 formtype如何传递数据,php,forms,symfony,object,Php,Forms,Symfony,Object,我想构建一个表单,其中包含来自2个相关实体的数据,并且能够用一次提交更新这两个实体。到目前为止,我得到了一个工作表单,它使用formtype,但只使用静态数据(例如$foo='bar')。我不知道如何将数据从我的控制器传递到formtype并在我的formtype中使用它 这就是我得到的: // AdminController.php // throws exception $em = $this->getDoctrine()->getManager();
// AdminController.php
// throws exception
$em = $this->getDoctrine()->getManager();
$article = $em->getRepository('AcmeBlogBundle:Articles')->find($id);
$form = $this->createForm(new ArticlesType(), $article);
这会引发一个异常:
Expected argument of type "array or (\Traversable and \ArrayAccess)", "string" given
所以,我试着这么做
//this is working, but (of course) empty
$article = new Articles();
$form = $this->createForm(new ArticlesType(), $article);
顺便说一下,这是我的ArticlesType.php
class ArticlesType extends AbstractType {
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('headline', 'text',
array('label' => false))
->add('articleContent', 'textarea')
->add('state', 'entity',
array('class' => 'Acme\BlogBundle\Entity\Status',
'property' => 'status',
'label' => false))
->add('category', 'entity',
array('class' => 'Acme\BlogBundle\Entity\Categories',
'property' => 'cat_name',
'label' => false))
->add('save', 'submit')
->add('aufstellung', 'collection', array('type' => new AufstellungType()));
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Acme\BlogBundle\Entity\Articles',
));
}
public function getName()
{
return 'articles';
}
}
编辑:
请包含完整的异常消息。。。投掷的方法和课程在这里也很有趣。你是说这个吗?(请参见编辑)只要结束这一行,错误就消失了,尽管我没有访问我的AufstellungsType“->add('aufstellung','collection',array('type'=>new AufstellungType());“您的关系可能设置不正确。发布文章实体代码。除了aufstellung之外,请省略所有属性。包括构造函数。
CRITICAL - Uncaught PHP Exception Symfony\Component\Form\Exception\UnexpectedTypeException: "Expected argument of type "array or (\Traversable and \ArrayAccess)", "string" given" at C:\projects\my_project_name\vendor\symfony\symfony\src\Symfony\Component\Form\Extension\Core\EventListener\ResizeFormListener.php line 84