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 CarBrandBundleFormType;
use SymfonyComponentFormAbstractType;
use SymfonyComponentFormFormBuilderInterface;
use SymfonyComponentOptionsResolverOptionsResolverInterface;
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 CarBrandBundleController;
use CarBrandBundleEntityBrands;
use CarBrandBundleEntityCars;
use CarBrandBundleFormTypeBothType;
use SymfonyBundleFrameworkBundleControllerController;
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] => CarBrandBundleEntityBrands Object
(
[id:protected] =>
[name:protected] =>
[origin:protected] =>
)
[1] => CarBrandBundleEntityCars Object
(
[id:protected] =>
[model:protected] =>
[price:protected] =>
)
[brands] => CarBrandBundleEntityBrands Object
(
[id:protected] =>
[name:protected] => Mercedes
[origin:protected] => Germany
)
[cars] => CarBrandBundleEntityCars Object
(
[id:protected] =>
[model:protected] => SL500
[price:protected] => 25,000
)
)
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…