Symfony表单创建新对象并创建第一个一对多对象 [英] Symfony form creates new object and create first one-to-many object

查看:92
本文介绍了Symfony表单创建新对象并创建第一个一对多对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个实体支持票:SupportTicket()。我还有一个条目来回复每张票:SupportEntry()。我在SupportTicket()和SupportEntry()之间建立一对多的关系。



现在我要做的是构建我的表单,以便创建初始的SupportTicket然后插入第一个SupportEntry,所有的都是相同的形式。我一直在乱写代码一段时间,只有一半了解我在做什么,但这是我现在所在的地方:

  //我的控制器,创建窗体
$ supportTicket = new SupportTicket();

$ form = $ this-> createFormBuilder($ supportTicket)
- > add('subject','text',array(
'label'=> '主题'
))
- > add('jobNumber','text',array(
'label'=>'作业号'
))
- > add('supportGroup','entity',array(
'label'=>'Group',
'class'=>'ShawmutClientBundle:SupportGroup',
'property'=>'name',
'multiple'=> true,
'expanded'=> true
))
// - > add ('supportEntries',new SupportEntryType())
- > add('supportEntries',new SupportEntryType())

- > add('Save','submit')

- > getForm();

我尝试在自定义表单类型

 <?php 
命名空间Shawmut\ClientBundle\Form\Type;

使用Symfony\Component\Form\AbstractType;
使用Symfony\Component\Form\FormBuilderInterface;
使用Symfony\Component\OptionsResolver\OptionsResolverInterface;

class SupportEntryType extends AbstractType
{
public function buildForm(FormBuilderInterface $ builder,array $ options)
{
$ builder-> add('评论','textarea');
}

public function setDefaultOptions(OptionsResolverInterface $ resolver)
{
$ resolver-> setDefaults(array(
'data_class'=> Shawmut\ClientBundle\Entity\SupportEntry',
));
}

public function getName()
{
return'SupportEntryType';
}
}

该表单确实有我的评论框从表单类型拉入,但是当我尝试提交表单时,我收到这个错误:


属性supportEntries存在setSupportEntries(),_ set()或 _call()的方法之一,并在类Me\MyBundle\Entity\SupportTicket中具有公共访问权限。 / p>

是的,这是有道理的。应该是那里的addSupportEntries()方法。那么如何告诉表单构建器使用addSupportEntries而不是setSupportEntries?



提前感谢

解决方案

集合表单输入。

   - > add(
'supportEntries',
'collection',
array(
'type'=> new SupportEntryType(),
'标签'=>'支持条目',
'error_bubbling'=> true,
'cascade_validation'=> true,



I have an entity for support tickets: SupportTicket(). I also have an entry for replies to each ticket: SupportEntry(). I setup a one-to-many relationship between SupportTicket() and SupportEntry().

Now what I'm trying to do is build my form so that it creates the initial SupportTicket and then inserts the first SupportEntry, all in the same form. I've been messing around with my code for a while, only half-understanding what I'm doing, but this is where I'm at right now:

// My controller, creating the form
$supportTicket = new SupportTicket();

    $form = $this->createFormBuilder($supportTicket)
        ->add('subject', 'text', array(
            'label'         => 'Subject'
        ))
        ->add('jobNumber', 'text', array(
            'label'         => 'Job Number'
        ))
        ->add('supportGroup', 'entity', array(
            'label'         => 'Group',
            'class'         => 'ShawmutClientBundle:SupportGroup',
            'property' =>   'name',
            'multiple'      => true,
            'expanded'      => true
        ))
       // ->add('supportEntries', new SupportEntryType())
        ->add('supportEntries', new SupportEntryType())

        ->add('Save', 'submit')

        ->getForm();

My attempt at the custom form type

<?php
namespace Shawmut\ClientBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class SupportEntryType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
    $builder->add('comment', 'textarea');
}

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'Shawmut\ClientBundle\Entity\SupportEntry',
    ));
}

public function getName()
{
    return 'SupportEntryType';
}
}

The form does have the comment box that I've pulled in from the form type, but when I try to submit the form, I get this error:

Neither the property "supportEntries" nor one of the methods "setSupportEntries()", "_set()" or "_call()" exist and have public access in class "Me\MyBundle\Entity\SupportTicket".

And yeah, that makes sense. It should be the addSupportEntries() method which is there. So how do I tell the form builder to use addSupportEntries instead of setSupportEntries?

Thanks in advance

解决方案

Give the collection form type a go.

->add(
    'supportEntries',
    'collection',
    array(
        'type' => new SupportEntryType(),
        'label' => 'Support Entries',
        'error_bubbling' => true,
        'cascade_validation' => true,
    )
)

这篇关于Symfony表单创建新对象并创建第一个一对多对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆