Symfony REST API form collection with many-to-many entities
<p>I'm trying to make a Symfony form with a many-to-many relationship using hidden entities. With <a href="https://gist.github.com/bjo3rnf/4061232" rel="nofollow">this gist</a>, my form connects plain IDs to entity objects, and has worked quite well for the REST API I'm building. The problem is how to create a form to receive and connect multiple entities when the embedded entity is only a single ID.</p>

<p>For example,</p>

<pre><code>Employee
    $id - primary key int
    $name
    $companies - many-to-many relationship

Company
    $id - primary key int
    $name
    $employees - inverse many-to-many relationship
</code></pre>

<p>Now I want to receive a POST request with this content</p>

<pre><code>name = "Employee 1"
companies[] = 1
companies[] = 2
</code></pre>

<p>You can see that the company form only contains integers that map to existing companies. A normal <a href="http://symfony.com/doc/current/cookbook/form/form_collections.html" rel="nofollow">Symfony collection</a> assumes that you're joining another object together with new properties, like name, email or phone number, but in this case the ID is the only item to match.</p>

<p>How can I configure a collection form type to take in only an ID and still work without web forms?</p>