I have two entity classes in a unidirectional many-to-one relation and two fos rest controllers that take care of creating, fetching, updating, and deleting each of them using their own form types (as described here).
When creating a new entity on the "many" side of the relation (Topic), I want to link it with an existing entity on the "one" side (Category). The form type of Topic looks like this:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('category', 'entity', array('class' => 'MyNiceBundle:Category', 'property' => 'id'))
;
}
but it doesn't take my post request of the form
{"my_form":{"category":{"id":2, "aProperty":"bla"}}}
or
{"my_form":{"category":{"id":2}}}
The error I get is "category":{"errors":["This value is not valid."]}}
In the end, I found out that sending
{"my_form":{"category":2}}
actually worked. But it's a bit annoying because when I fetch Topics using GET, I get
{"id":1, "category":{"id":2, "aProperty":"bla"}}
and not
{"id":1, "category":2}
so before making an update with PUT, I have to overwrite the category property with its ID...
I'm sure there is a better way to do this. Any advice?