title: What's the propper way to insert data one to many relation symfony4?
body: <p>It's simple im doing a permission table from Companies and Users where i have to store the user id and the company id right now i have this and i get this error </p>
  
  <blockquote>
    <p>Expected value of type "App\Entity\CompanyUserPermissionMap" for association field "App\Entity\User#$companyUserPermissionMaps", got "App\Entity\Company" instead.</p>
  </blockquote>
  
  <p>User Entity</p>
  
  <pre><code>/**
   * @ORM\Id()
   * @ORM\GeneratedValue()
   * @ORM\Column(type="integer")
   */
  private $userId;
  
  /**
   * @ORM\Column(type="string", length=12)
   */
  private $code;
  
  /**
   * @ORM\Column(type="string", length=180, unique=true)
   */
  private $email;
  
  
  /**
   * @var CompanyUserPermissionMap[]
   * @ORM\OneToMany(targetEntity="App\Entity\CompanyUserPermissionMap", mappedBy="user", orphanRemoval=true)
   */
  private $companyUserPermissionMaps;
  
  
  public function __construct()
  {
      $this-&gt;companyUserPermissionMaps = new ArrayCollection();
  }
  
  
  public function getCompanyUserPermissionMaps(): Collection
  {
      return $this-&gt;companyUserPermissionMaps;
  }
  
  public function addCompanyUserPermissionMaps(CompanyUserPermissionMaps $permission): self
  {
      if (!$this-&gt;companyUserPermissionMaps-&gt;contains($permission)) {
          $this-&gt;companyUserPermissionMaps[] = $permission;
          $permission-&gt;setUser($this);
      }
  
      return $this;
  }
  </code></pre>
  
  <p>Company Entity</p>
  
  <pre><code>#########################
  ##      PROPERTIES     ##
  #########################
  
  /**
   * @ORM\Id()
   * @ORM\GeneratedValue()
   * @ORM\Column(type="integer")
   */
  private $companyId;
  
  /**
   * @ORM\Column(type="string", length=6, unique=true)
   */
  private $code;
  
  
  
  /**
   * @var CompanyUserPermissionMap[]
   * @ORM\OneToMany(targetEntity="App\Entity\CompanyUserPermissionMap", mappedBy="company", orphanRemoval=true)
   */
  private $companyUserPermissionMaps;
  
  
  public function __construct()
  {
      $this-&gt;companyUserPermissionMaps = new ArrayCollection();
  
  }
  
  
  
  /**
   * @return Collection|CompanyUserPermissionMaps[]
   */
  public function getCompanyUserPermissionMaps(): Collection
  {
      return $this-&gt;companyUserPermissionMaps;
  }
  
  public function addCompanyUserPermissionMaps(AccountingBankPermission $permission): self
  {
      if (!$this-&gt;companyUserPermissionMaps-&gt;contains($permission)) {
          $this-&gt;companyUserPermissionMaps[] = $permission;
          $permission-&gt;setAccount($this);
      }
  
      return $this;
  }
  
  public function removeCompanyUserPermissionMaps(AccountingBankPermission $permission): self
  {
      if ($this-&gt;companyUserPermissionMaps-&gt;contains($permission)) {
          $this-&gt;companyUserPermissionMaps-&gt;removeElement($permission);
          // set the owning side to null (unless already changed)
          if ($permission-&gt;getAccount() === $this) {
              $permission-&gt;setAccount(null);
          }
      }
  
      return $this;
  }
  </code></pre>
  
  <p>relation table</p>
  
  <pre><code>Column(type="integer")
  
  private $companyUserPermissionId;
  
  
  /**
   * @var User
   * @ORM\ManyToOne(targetEntity="App\Entity\User", inversedBy="companyUserPermissionMaps")
   * @ORM\JoinColumn(referencedColumnName="user_id", nullable=false)
   */
  private $user;
  
  /**
   * @var Company
   * @ORM\ManyToOne(targetEntity="App\Entity\Company", inversedBy="companyUserPermissionMaps")
   * @ORM\JoinColumn(referencedColumnName="company_id", nullable=true)
   */
  private $company;
  
  
  /**
   * @return int|null
   */
  public function getCompanyUserPermissionId(): ?int
  {
      return $this-&gt;companyUserPermisionId;
  }
  
  
  
  /**
   * @return User
   */
  public function getUser(): ?User
  {
      return $this-&gt;user;
  }
  
  /**
   * @param User $user
   * @return AccountingBankPermission
   */
  public function setUser(?User $user): self
  {
      $this-&gt;user = $user;
      return $this;
  }
  
  /**
   * @return Company
   */
  public function getCompany(): ?Company
  {
      return $this-&gt;company;
  }
  
  /**
   * @param array $company
   * @return CompanyUserPermissionMap
   */
  public function setCompany(?array $company): self
  {
      $this-&gt;company = $company;
      return $this;
  }
  </code></pre>
  
  <p>Form type</p>
  
  <pre><code> $builder
              -&gt;add('roles' ,ChoiceType::class ,[
                  'required'  =&gt; true,
                  'choices' =&gt; $this-&gt;roles,
                  'multiple' =&gt; true,
                  'expanded' =&gt; true,
                  'label_attr' =&gt; [
                      'class' =&gt; 'custom-control-label',
                  ],
                  'choice_attr' =&gt; function($val, $key, $index) {
                      return ['class' =&gt; 'custom-control-input'];
                  },
                  'attr'=&gt;['class' =&gt;'custom-checkbox custom-control']
              ])
              -&gt;add('email', EmailType::class, [
                  'label' =&gt; "E-Mail"
              ])
              -&gt;add('firstName', TextType::class, [
                  'label' =&gt; "First Name"
              ])
              -&gt;add('lastName', TextType::class, [
                  'label' =&gt; "Last Name"
              ])
              -&gt;add('companyUserPermissionMaps' ,EntityType::class ,[
                  'required' =&gt; true,
                  'class'    =&gt; Company::class,
                  'label'    =&gt; 'Compañia',
                  'multiple' =&gt; true,
                  'expanded' =&gt; false,
                  'choice_label'  =&gt; 'legalName',
                  'mapped'=&gt;false
              ])
              -&gt;add('save', SubmitType::class, [
                  'label' =&gt; "Save"
              ])
  </code></pre>
  
  <p>and my controller function looks like this </p>
  
  <pre><code> $user = new User();
  
      $originalRoles = $this-&gt;getParameter('security.role_hierarchy.roles');
  
      $options=['roles' =&gt; $originalRoles ];
  
      $form = $this-&gt;createForm(UserType::class, $user, $options);
  
      $form-&gt;handleRequest($request);
  
      if ($form-&gt;isSubmitted() &amp;&amp; $form-&gt;isValid()) {
  
          $tempPassword = "some pass";
  
          $user-&gt;setPassword($encoder-&gt;encodePassword(
              $user,
              $tempPassword
          ));
  
          $companies=[];
          $companiesForm=$form-&gt;get('companyUserPermissionMaps')-&gt;getData();
          foreach ($companiesForm as $value) {
              $companies[] = $value-&gt;getCompanyId();
          }
  
          // Save object to database
          $entityManager = $this-&gt;getDoctrine()-&gt;getManager();
  
          /** @var CompanyRepository $companyRepository */
          $companyRepository = $this-&gt;getDoctrine()-&gt;getRepository(Company::class);
          $companiesArr =$companyRepository-&gt;findCompanyByArray($companies);
          $companyUserPermissionMap = new CompanyUserPermissionMap();
          $companyUserPermissionMap-&gt;setUser($user);
          $companyUserPermissionMap-&gt;setCompany($companiesArr);
  
          $entityManager-&gt;persist($user);
          $entityManager-&gt;persist($companyUserPermissionMap);
  </code></pre>