Symfony 为表单类型中的数据类赋值

Symfony 为表单类型中的数据类赋值,symfony,Symfony,我有一个表单类型,希望知道在我下面的例子中,在setDefaultOptions中针对data\u class应该做什么。我知道我们通常放置实体的路径,但在这种情况下,我嵌入了两个实体,那么我现在该怎么做 我知道我们可以忽略它,但我不想像SensioLabs()建议的那样 表单类型: namespace Car\BrandBundle\Form\Type; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\F

我有一个表单类型,希望知道在我下面的例子中,在
setDefaultOptions
中针对
data\u class
应该做什么。我知道我们通常放置实体的路径,但在这种情况下,我嵌入了两个实体,那么我现在该怎么做

我知道我们可以忽略它,但我不想像SensioLabs()建议的那样

表单类型:

namespace Car\BrandBundle\Form\Type;

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

class BothType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->setMethod('POST')
            ->setAction($options['action'])
            ->add('brands', new BrandsType())
            ->add('cars', new CarsType())
            ->add('button', 'submit', array('label' => 'Add'))
            ;
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array('data_class' => '?????????????????????'));
    }

    public function getName()
    {
        return 'both';
    }
} 
namespace Car\BrandBundle\Controller;

use Car\BrandBundle\Entity\Brands;
use Car\BrandBundle\Entity\Cars;
use Car\BrandBundle\Form\Type\BothType;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class BothController extends Controller
{
    public function indexAction()
    {
        $entity = array(new Brands(), new Cars());

        $form = $this->createForm(new BothType(), $entity,
                array('action' => $this->generateUrl('bothCreate')));

        return $this->render('CarBrandBundle:Default:both.html.twig',
            array('page' => 'Both', 'form' => $form->createView()));
    }
}
控制器:

namespace Car\BrandBundle\Form\Type;

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

class BothType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->setMethod('POST')
            ->setAction($options['action'])
            ->add('brands', new BrandsType())
            ->add('cars', new CarsType())
            ->add('button', 'submit', array('label' => 'Add'))
            ;
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array('data_class' => '?????????????????????'));
    }

    public function getName()
    {
        return 'both';
    }
} 
namespace Car\BrandBundle\Controller;

use Car\BrandBundle\Entity\Brands;
use Car\BrandBundle\Entity\Cars;
use Car\BrandBundle\Form\Type\BothType;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class BothController extends Controller
{
    public function indexAction()
    {
        $entity = array(new Brands(), new Cars());

        $form = $this->createForm(new BothType(), $entity,
                array('action' => $this->generateUrl('bothCreate')));

        return $this->render('CarBrandBundle:Default:both.html.twig',
            array('page' => 'Both', 'form' => $form->createView()));
    }
}
当我回显提交的数据时,我将获得以下复制数据:

Array
(
    [0] => Car\BrandBundle\Entity\Brands Object
        (
            [id:protected] => 
            [name:protected] => 
            [origin:protected] => 
        )

    [1] => Car\BrandBundle\Entity\Cars Object
        (
            [id:protected] => 
            [model:protected] => 
            [price:protected] => 
        )

    [brands] => Car\BrandBundle\Entity\Brands Object
        (
            [id:protected] => 
            [name:protected] => Mercedes
            [origin:protected] => Germany
        )

    [cars] => Car\BrandBundle\Entity\Cars Object
        (
            [id:protected] => 
            [model:protected] => SL500
            [price:protected] => 25,000
        )

)

我认为最好的方法是忽略它,因为没有特定的实体可以链接它。该文档可能应理解为“如果您的表单绑定到某个实体,则最好是”

在您的情况下,您没有实体类来映射它。所以除非您创建一个,否则应该将其留空。是的,我忽略了控制器中传递的数组,并将
null
分配给
data\u class
,这是正常的。