Assigning value to data_class in form type

2019-09-06 09:35发布

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?

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' => '?????????????????????'));

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
        )

)

标签: symfony
1条回答
Bombasti
2楼-- · 2019-09-06 10:13

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".

查看更多
登录 后发表回答