我有兩個實體配置文件和汽車。配置文件有很多汽車。當我嘗試嵌入CarsFormType到ProfileFOrmType,我得到這個錯誤:Symfony2中的相關表單和選項「小部件」不存在
The form's view data is expected to be an instance of class Acme\UserBundle\Entity\Car, but is an instance of class Doctrine\Common\Collections\ArrayCollection.
我試圖找到解決辦法,但我沒有看到任何有用的結果。創建簡介時,我只想創建一輛車。
我的模型代碼:
簡介:
/**
* @ORM\OneToMany(targetEntity="Car", mappedBy="profile", cascade={"persist"})
*/
private $cars;
ProfileFormType:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('fio', null, array('label' => 'ФИО'));
$builder->add('birthDate', null, array('label' => 'Дата рождения', 'widget' => 'single_text'));
$builder->add('file', 'file', array('label' => 'Фото', 'required' => false));
$builder->add('telephone', null, array('label' => 'Телефон'));
$builder->add('contactMail', null, array('label' => 'Контактная почта'));
/* $builder->add('category', 'choice', array(
'label' => 'Категория прав',
'choices' => array('B' => 'Категория B', 'C' => 'Категория C',),
'expanded' => true,
'multiple' => true,
));*/
$builder->add('driverLicence', null, array('label' => 'Лицензия', 'widget' => 'single_text', 'required' => false));
$builder->add('yearOnRoad', null, array('label' => 'Год за рулем', 'required' => false));
$builder->add('cars', new CarType(), array('label' => 'Авто', 'required' => false));
// $builder->add('cars', 'entity', array('label' => 'Авто', 'required' => false, 'class' => 'VputiUserBundle:Car'));
// $builder->add('cars', 'collection', array('label' => 'Авто', 'required' => false, 'type' => new CarType()));
$builder->add('submit', 'submit');
}
/**
* {@inheritDoc}
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Vputi\UserBundle\Entity\Profile',
'cascade_validation' => true,
));
}
/**
* {@inheritDoc}
*/
public function getName()
{
return 'profile_driver';
}
當汽車形成I型raplece這樣的:
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Acme\UserBundle\Entity\Car',
'cascade_validation' => true,
));
}
這樣:
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => null,
'cascade_validation' => true,
));
}
我得到新的錯誤:
The option "widget" does not exist. Known options are: "action", "attr", "auto_initialize", "block_name", "by_reference", "cascade_validation", "compound", "constraints", "csrf_field_name", "csrf_message", "csrf_protection", "csrf_provider", "csrf_token_id", "csrf_token_manager", "data", "data_class", "disabled",
等等哪裏是我的問題嗎?
但你有沒有讀過其他的錯誤信息?它明確指出:'通過將「data_class」選項設置爲null或通過添加將Doctrine \ Common \ Collections \ ArrayCollection類的實例轉換爲Acme \ UserBundle \ Entity \ Car實例的視圖轉換器,可以避免此錯誤。 ' – Ohgodwhy 2014-10-27 07:56:34
當我設置data_class爲空我得到的錯誤: '選項「小部件」不存在.' – nowiko 2014-10-27 07:58:55
這是一個不同於你所描述的問題..你應該關閉它,並提出一個新的問題,或編輯標題以反映真正的問題。你還應該包含你的代碼。 – Ohgodwhy 2014-10-27 08:00:51