title: Symfony best practice for join tables ManyToMany
body: <p>I am relative new to Symfony (and PHP in general) and I was wondering how to accomplish the following:</p>
  
  <p>I have 3 tables:</p>
  
  <pre><code>USER
    - id
    - lastNm
    - firstNm
    - username
    - isActive
  
  COMPANY
    - id
    - name
    - isActive
  
  COMPANY_USER
    - id
    - user
    - company
    - email
    - isActive
  </code></pre>
  
  <p>I want to create a rest api. 
  Users can be created with a new company or can be added to an existing company. When a company is created, it's for a new or existing user. I need to add several properties to the COMPANY_USER table.</p>
  
  <p>Which controllers do I need and what functions do I put in them? Especially for inserting, updating and deleting records. </p>
  
  <p>The things I have in mind are too complicated, there must be a more elegant way to do this...</p>
  
  <p>UPDATE:</p>
  
  <p>UserController:</p>
  
  <pre><code>  /**
     * @Route("/user/add", name="user_add")
     * @Method("POST")
     */
    public function addAction(Request $request, UserPasswordEncoderInterface $passwordEncoder, SerializerInterface $serializer)
    {
      $data = $request-&gt;getContent();
      $user = $serializer-&gt;deserialize($data, User::class, 'json');
      $em = $this-&gt;getDoctrine()-&gt;getManager();
  
      $user-&gt;setPassword($passwordEncoder-&gt;encodePassword($user, $user-&gt;getPassword()));
  
      $em-&gt;persist($user);
  
      try {
        $em-&gt;flush();
      }
      catch (UniqueConstraintViolationException $e) {
        return new Response(
          'Unique constraint',
          Response::HTTP_CONFLICT
        );
      }
  
      return new Response(
        $serializer-&gt;serialize(
          $user,
          'json'),
        Response::HTTP_CREATED
      );
    }
  </code></pre>
  
  <p>CompanyController:</p>
  
  <pre><code>  /**
     * @Route("/company/add", name="company_add")
     * @Method("POST")
     */
    public function addAction(Request $request, SerializerInterface $serializer)
    {
      $data = $request-&gt;getContent();
      $company = $serializer-&gt;deserialize($data, Company::class, 'json');
      $em = $this-&gt;getDoctrine()-&gt;getManager();
  
      $em-&gt;persist($company);
  
      try {
        $em-&gt;flush();
      }
      catch (UniqueConstraintViolationException $e) {
        return new Response(
          'Unique constraint',
          Response::HTTP_CONFLICT
        );
      }
  
  
      return new Response(
        $serializer-&gt;serialize(
          $company,
          'json'),
        Response::HTTP_CREATED
      );
    }
  </code></pre>
  
  <p><strong>How should the addAction of CompanyUserController look like?</strong></p>