2014-10-27 22 views
0

我有兩個實體配置文件和汽車。配置文件有很多汽車。當我嘗試嵌入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", 

等等哪裏是我的問題嗎?

+0

但你有沒有讀過其他的錯誤信息?它明確指出:'通過將「data_class」選項設置爲null或通過添加將Doctrine \ Common \ Collections \ ArrayCollection類的實例轉換爲Acme \ UserBundle \ Entity \ Car實例的視圖轉換器,可以避免此錯誤。 ' – Ohgodwhy 2014-10-27 07:56:34

+0

當我設置data_class爲空我得到的錯誤: '選項「小部件」不存在.' – nowiko 2014-10-27 07:58:55

+0

這是一個不同於你所描述的問題..你應該關閉它,並提出一個新的問題,或編輯標題以反映真正的問題。你還應該包含你的代碼。 – Ohgodwhy 2014-10-27 08:00:51

回答

1

我只能假設配置文件和汽車之間的關係是oneToMany,這意味着在您的表單中,您需要使用CarTypes的集合,而不是您的cars字段的單個集合。這意味着當你形成請求cars變量時,它會得到和ArrayCollectionCar對象,而不是表單所期望的單個Car

變化..

$builder->add('cars', new CarType(), array('label' => 'Авто', 'required' => false)); 

要..

$builder->add('cars', 'collection', array(
    'label' => 'Авто', 
    'required' => false, 
    'type'  => new CarType(), 
)); 

可以多一點閱讀了關於embedding collections in the docs,爲collection type are here實際的選擇。

+0

這種方式只能添加標籤,而不能輸入字段 – nowiko 2014-10-27 10:38:10

+0

正如它在「在文檔中嵌入集合」頁面所述,您需要用汽車初始化對象或添加JavaScript,以便您可以添加/取下汽車。 – qooplmao 2014-10-27 10:42:18

+0

感謝幫忙的人:) – nowiko 2014-10-27 10:47:10

0

你正在嘗試實現是有一個Collection of forms

我認爲正確的方法來設置,這是用這個辦法:

$buider->add('cars', 'collection', array(
      'type' => new carType(), 
      'allow_add' => true, 
      'allow_delete' => true, 
     ) 
    ); 

你不與形式設置數組在那裏。汽車中需要的字段需要使用carType。 關於錯誤: Symfony期待一個Acme \ UserBundle \ Entity \ Car實體匹配相應的類型,而不是接收另一個東西。這就是爲什麼我試圖指出你在正確的方向。

請在這裏看看這個One to Many form example的實例。

就在打保存後,我看到Qoop與我的回答完全相同:)所以,只要問問你是否不能運行。

相關問題