我想构建一个包含来自2个相关实体的数据的表单,并且能够使用一个提交更新这两个实体。到目前为止,我得到了一个使用formtype但仅使用静态数据的工作表单(例如$ foo ='bar')。我不知道如何将数据从我的控制器传递到formtype并在我的formtype中使用它...
这就是我所拥有的:
// 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';
}
}
编辑:
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