I work with Symfony2 Forms and FOSRestBundle.
I am trying to store an object with a many-many relationship in the database.
I create a form with a collection field ( http://symfony.com/doc/master/cookbook/form/form_collections.html ) as follows:
class MainType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('name');
$builder->add('description');
$builder->add('others', 'collection', array(
'type' => new OtherType()
));
}
public function getName()
{
return '';
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Acme\SearchBundle\Entity\Main',
'csrf_protection' => false
));
}
}
class OtherType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('id');
}
public function getName()
{
return '';
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Acme\SearchBundle\Entity\Other',
'csrf_protection' => false
));
}
}
A collection of objects of type Other is stored in the database. And I do not want to store more objects of this type, just read and associate them with the main object.
When I process the form, I use this function:
private function processForm(Main $main, $new = false)
{
$new = true;
$statusCode = $new ? 201 : 204;
$form = $this->createForm(new MainType(), $main);
$form->bind($this->getRequest());
if ($form->isValid()) {
$mainValidated = $form->getData();
$em = $this->getDoctrine()->getEntityManager();
$em->persist($mainValidated);
$em->flush();
return $this->view($new ? $mainValidated : null, $statusCode);
}
return $this->view($form, 400);
}
The json code that I am sending from the Backbone.js client:
{"others":[{"id":1}, {"id":2}]}
Entities:
Xml:
<entity name="Acme\SearchBundle\Entity\Main" table="main">
<id name="id type="integer" column="id">
<generator strategy="IDENTITY"/>
</id>
<field name="name" type="integer" column="name" nullable="true"/>
<field name="description" type="integer" column="description" nullable="true"/>
<many-to-many field="others" target-entity="Other" inversed-by="mains">
<cascade>
<cascade-persist/>
</cascade>
<join-table name="main_has_other">
<join-columns>
<join-column name="main" referenced-column-name="id"/>
</join-columns>
<inverse-join-columns>
<join-column name="other" referenced-column-name="id"/>
</inverse-join-columns>
</join-table>
</many-to-many>
</entity>
Entity:
<?php
namespace Acme\SearchBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation\Type;
use JMS\Serializer\Annotation\Groups;
use JMS\Serializer\Annotation\Expose;
class Main
{
private $id;
private $name;
private $description;
private $others;
public function __construct()
{
$this->others = new \Doctrine\Common\Collections\ArrayCollection();
}
public function setName($name)
{
$this->name = $name;
return $this;
}
public function getName()
{
return $this->name;
}
public function setDescription($description)
{
$this->description = $description;
return $this;
}
public function getDescription()
{
return $this->description;
}
public function getId()
{
return $this->id;
}
public function addOthers(\Acme\SearchBundle\Entity\Other $other)
{
$this->others[] = $other;
return $this;
}
public function removeOthers(\Acme\SearchBundle\Entity\Other $other)
{
$this->others->removeElement($other);
}
public function getOthers()
{
return $this->others;
}
}
Xml:
<entity name="Acme\SearchBundle\Entity\Other" table="other">
<id name="id" type="integer" column="id">
<generator strategy="IDENTITY"/>
</id>
<field name="name" type="string" column="name" length="255" nullable="true"/>
<field name="description" type="string" column="name" length="255" nullable="true"/>
<many-to-many field="mains" target-entity="Main" mapped-by="others">
<cascade>
<cascade-persist/>
</cascade>
</many-to-many>
</entity>
Entity:
<?php
namespace Acme\SearchBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation\Type;
use JMS\Serializer\Annotation\Groups;
class Other
{
private $id;
private $name;
private $description;
private $mains;
public function __construct()
{
$this->mains = new \Doctrine\Common\Collections\ArrayCollection();
}
public function setName($name)
{
$this->name = $name
}
public function getName()
{
return $this->name;
}
public function setDescription($description)
{
$this->description = $description;
return $this;
}
public function getDescription()
{
return $this->description;
}
public function getId()
{
return $this->id;
}
public function setId($id)
{
$this->id = $id;
return $this;
}
public function addMains(\Acme\SearchBundle\Entity\Main $main)
{
$this->mains[] = $main;
return $this;
}
public function removeMains(\AcmeSearchBundle\Entity\Main $main)
{
$this->mains->removeElement($main);
}
public function getMains()
{
return $this->mains;
}
}
"main" , - . , "" .
.