在表单类型中为 data_class 赋值 [英] Assigning value to data_class in form type

查看:30
本文介绍了在表单类型中为 data_class 赋值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表单类型,想知道在我下面的例子中在 setDefaultOptions 中对 data_class 放置什么.我知道我们通常会放置实体的路径,但在这种情况下,我嵌入了两个实体,那么我现在该怎么办?

I have a Form Type and wish to know what to put against data_class in setDefaultOptions in my case below. I know that we normally put the path of our entity but in this case I have two entities embedded so what do I do now?

我知道我们可以忽略它,但我不想,因为 SensioLabs 建议不要这样做 (...因此,虽然并非总是必要的,但显式指定 data_class 选项通常是个好主意...).

I know that we can leave ignore it but I don't want to as it's suggested not to by SensioLabs (...So, while not always necessary, it's generally a good idea to explicitly specify the data_class option...).

$resolver->setDefaults(array('data_class' => '?????????????????????'));

表单类型:

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()));
    }
}

当我回显提交的数据时,我得到了这个复制的数据:

When I echo submitted data I'm getting this replicated data:

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
        )

)

推荐答案

我认为这里最好的方法实际上是忽略它,因为没有特定的实体可以链接它.该文档可能应该被理解为如果您的表单绑定到一个实体,则最好".

I think the best way here is actually to ignore it as there are no specific entity to link it to. The doc should probably be read as "if your form is bound to an entity it is better to".

这篇关于在表单类型中为 data_class 赋值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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