php - Symfony form CollectionType Field -
hi i'm quite new in symfony 2. i'm dealing problem several hours now, i'm forced ask comunity.
i have 2 entities
user --> language (onetomany)
user entity
/** * * @orm\onetomany(targetentity="user_language", mappedby="user") */ private $languages; public function __construct() { $this->languages = new arraycollection(); }
user_language entity
/** @orm\column(type="string", length=50) */ private $language; /** * @orm\manytoone(targetentity="user", inversedby="languages") */ private $user;
user_languagetyp
public function buildform(formbuilderinterface $builder, array $options) { $builder ->add( 'language' ); }
i'm trying build form user can add/edit/delete speaking languages
user_language in database
when getting data database (in controller) - $user->getlanguages() got persistantcollection, querybuilder() got array
is there way, how pass createform() function?
$form = $this->createform( user_languagetype::class, $user->getlanguages() );
i'm getting 2 erros:
the form's view data expected instance of class arpanetruzicja7estatesbundle\entity\user_language, instance of class doctrine\orm\persistentcollection.
or
the form's view data expected instance of class arpanetruzicja7estatesbundle\entity\user_language, a(n) array. can avoid error setting "data_class" option null or adding view transformer transforms a(n) array instance of arpanetruzicja7estatesbundle\entity\user_language.
setting 'data_class' => null
, did not help.
thanks lot.
if want allow user add/delete number of languages, have use collection type field. http://symfony.com/doc/2.7/reference/forms/types/collection.html
here use it: http://symfony.com/doc/2.7/form/form_collections.html
you create form user entity, add collection field type on user_languagetype, , have add javascript manage adding , deleting language.
i don't write example, symfony doc perfect :)
but assume have form/usertype, can add collection field :
->add('tags', 'collection', [ 'type' => new user_languagetype(), 'allow_add' => true, 'allow_delete' => true, ]);
(for symfony3)
->add('tags', 'collection', [ 'type' => user_languagetype::class, 'allow_add' => true, 'allow_delete' => true, ]);
the array short syntaxe [] needs php5.4 min, if not use array()
Comments
Post a Comment