phpformssymfonyarraycollection

Symfony forms with multiple arrayCollection


Every user has multiple sports so I have created table users and table sports and table usersport.

I want that the user select multiple sport and store it in table user sport

user entity arrayCollection
/**
 * @ORM\OneToMany(targetEntity="SportUser", mappedBy="user")
 */
private $sports;
public function __construct()
{       
    $this->sports = new ArrayCollection();
}

User type:

->add('sports', CollectionType::class, [
'entry_type'    => SportType::class,
    'allow_add'     => true,
    'allow_delete'  => true,
 ]) 

I want to make a user form with multi-select from entity sport and store it in table user sport


Solution

  • The best thing is to use the EntityType

    ->add('sports', EntityType::class, [
        'class'    => SportType::class,
        'choice_label' => 'sport_field'
        'multiple'     => true
    ])