I have an entity Serveur
which have a ManyToOne relation with Responsable
class Serveur implements NotifyPropertyChanged
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var \MyBundle\Entity\Responsable
*
* @ORM\ManyToOne(targetEntity="\MyBundle\Entity\Responsable", inversedBy="serveurs", cascade={"persist"})
* @ORM\JoinColumn(name="serveur_responsable", referencedColumnName="id", nullable=true)
*
*/
private $responsable;
class Responsable
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="Uid", type="string", unique=true, nullable=false)
*/
private $uid;
When you enter a new Responsable, in my updateAction (serveur), I call a service which look if there's already a Responsable with this uid, and If not I return a new Responsable :
public function updateAction(Request $request, $id)
{
$entity = $em->getRepository('MyBundle:Serveur')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Serveur entity.');
}
$editForm = $this->createEditForm($entity);
$editForm->handleRequest($request);
if ($editForm->isValid()) {
$resp = $editForm['responsable']->getData();
if($resp->getUid()){
$entity_resp = $this->container->get('app.responsable')->getResp($resp->getUid());
$entity->setResponsable($entity_resp);
}
else{
$entity->setResponsable(null);
}
Now, when I edit a serveur, if there's no responsable and I enter one, it's working wether the responsable exist or not.
But If I edit a serveur which already have a responsable, and I want to setResponsable to null, it's not working :
An exception occurred while executing 'UPDATE responsable SET Uid = ? WHERE id = ?' with params [null, 192]: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '' for key 'UNIQ_52520D076BD640E6'
It's actually trying to UPDATE the uid of the responsable to null, but that's not what I want, I'd like to keep the responsable in the DB and just remove the relation..
Am I missing something ?
Thanks !