简体   繁体   English

在表单类型中为data_class赋值

[英]Assigning value to data_class in form type

I have a Form Type and wish to know what to put against data_class in setDefaultOptions in my case below. 我有一个表单类型,并希望在下面的情况下知道对setDefaultOptions中的data_class什么。 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? 我知道我们通常放置实体的路径,但是在这种情况下,我嵌入了两个实体,那么我现在该怎么办?

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... ). 我知道我们可以忽略它,但我不希望这样做,因为SensioLabs建议不要这样做( ...因此,虽然并非总是必要,但明确指定data_class选项是一个好主意

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

Form type: 表格类型:

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';
    }
} 

Controller: 控制器:

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". 该文档可能应理解为“如果您的表单绑定到实体,则更好”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM