[英]Assigning value to data_class in form type
我有一个表单类型,并希望在下面的情况下知道对setDefaultOptions
中的data_class
什么。 我知道我们通常放置实体的路径,但是在这种情况下,我嵌入了两个实体,那么我现在该怎么办?
我知道我们可以忽略它,但我不希望这样做,因为SensioLabs建议不要这样做( ...因此,虽然并非总是必要,但明确指定data_class选项是一个好主意 。
$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()));
}
}
当我回显提交的数据时,我得到的是复制的数据:
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
)
)
我认为最好的方法实际上是忽略它,因为没有特定的实体可以链接到它。 该文档可能应理解为“如果您的表单绑定到实体,则更好”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.