简体   繁体   中英

Symfony2 form collection of one entity

I have an entity called 'Candidate'. It is not that special. On its own, it's doing fine: I can make a form from a type and persist it.

class CandidateType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('name', TextType::class, array('label' => 'Naam'))
        ->add('openPosition', EntityType::class, array('class' => 'AppBundle:OpenPosition','choice_label' => 'subject','label' => 'Functie'))
        ;

}

However, I want to be able to make a collection of this form so that users will be able to create new candidates with Javascript over and over and save them all at once. I've already done some work on another collection (entity that has a one-to-many relation with another entity, with a prototype and some nice Javascript) and that works fine, but I seem to be unable to create a collection of just one entity (which actually sounds quite like a paradox, perhaps I'm on a wrong approach).

Any thoughts on this?

From the symfony documentation - you can embed a collection of forms using jquery. By using the data prototype and many javascript/jquery, you should be able to do the tricks.

Here is the doc, I use it to manage multiple adress add for a user and it works well !

http://symfony.com/doc/current/form/form_collections.html#allowing-new-tags-with-the-prototype

But for sure, you will have to create a custom multipleCreateAction() to handle the multiple creation of candidates setted.

I've solved it the way Alvaro suggested : by creating an ArrayCollection of candidates:

$candidates['candidates'] = new ArrayCollection();

I then handled it like any other formcollection.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM