[英]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.