简体   繁体   中英

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

)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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