phpformssymfonyarraycollectionsymfony-4.3

symfony 4 form with multiple arrayCollection


well, every user has multiple sports so i have created table users and table sports and table usersport

so 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();
}

//usertype

->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 ])