Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 UniqueConstraint and UniqueEntity (Integrity Violation)

I've got a little problem with a Doctrine entity for a few days now. I've defined it with a UniqueConstraint on two fields with a UniqueEntity Validation on these two fields after that. But after validating of my form with a adding of an entity already in base, no way to obtain my error message in my form.

Just an error message from Symfony :

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '339057986-00012' for key 'SIRET'

Here is my entity declaration, all seems fine to me but maybe have I forgotten or misunderstood something?

<?php

namespace Proetco\FrontBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * Proetco\FrontBundle\Entity\Entreprise
 *
 * @ORM\Table(name="entreprise", uniqueConstraints={@ORM\UniqueConstraint(name="SIRET", columns={"SIREN", "NIC"})}) 
 * @ORM\Entity(repositoryClass="Proetco\FrontBundle\Entity\EntrepriseRepository")
 * @UniqueEntity(fields={"SIREN","NIC"}, message="Cette entreprise est déjà enregistrée")
 */
class Entreprise {

protected $Siret;

/**
 * @var integer $id
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * @var string $SIREN
 *
 * @ORM\Column(name="SIREN", type="string", length=9)
 */
private $SIREN;

/**
 * @var string $NIC
 *
 * @ORM\Column(name="NIC", type="string", length=5)
 */
private $NIC;

/**
 * @var string $RS
 *
 * @ORM\Column(name="RS", type="string", length=45, nullable=true)
 */
private $RS;

/**
 * @var string $NCOM
 *
 * @ORM\Column(name="NCOM", type="string", length=45, nullable=true)
 */
private $NCOM;

/**
 * @var string $CPOS
 *
 * @ORM\Column(name="CPOS", type="string", length=5, nullable=true)
 */
private $CPOS;

/**
 * @var string $LCOM
 *
 * @ORM\Column(name="LCOM", type="string", length=45, nullable=true)
 */
private $LCOM;

/**
 * @var string $INSEE
 *
 * @ORM\Column(name="INSEE", type="string", length=5, nullable=true, nullable=true)
 */
private $INSEE;

/**
 * @var string $DEP
 *
 * @ORM\Column(name="DEP", type="string", length=2)
 */
private $DEP;

/**
 * @var string $ARR
 *
 * @ORM\Column(name="ARR", type="string", length=1, nullable=true)
 */
private $ARR;

/**
 * @var string $CAN
 *
 * @ORM\Column(name="CAN", type="string", length=2, nullable=true)
 */
private $CAN;

public function getSiret()
{
    return $this->Siret;
}

public function setSiret($Siret)
{
    $this->Siret = $Siret;
}

/**
 * Get id
 *
 * @return integer 
 */
public function getId()
{
    return $this->id;
}

/**
 * Set SIREN
 *
 * @param string $sIREN
 */
public function setSIREN($sIREN)
{
    $this->SIREN = $sIREN;
}

/**
 * Get SIREN
 *
 * @return string 
 */
public function getSIREN()
{
    return $this->SIREN;
}

/**
 * Set NIC
 *
 * @param string $nIC
 */
public function setNIC($nIC)
{
    $this->NIC = $nIC;
}

/**
 * Get NIC
 *
 * @return string 
 */
public function getNIC()
{
    return $this->NIC;
}

/**
 * Set RS
 *
 * @param string $rS
 */
public function setRS($rS)
{
    $this->RS = $rS;
}

/**
 * Get RS
 *
 * @return string 
 */
public function getRS()
{
    return $this->RS;
}

/**
 * Set NCOM
 *
 * @param string $nCOM
 */
public function setNCOM($nCOM)
{
    $this->NCOM = $nCOM;
}

/**
 * Get NCOM
 *
 * @return string 
 */
public function getNCOM()
{
    return $this->NCOM;
}

/**
 * Set CPOS
 *
 * @param string $cPOS
 */
public function setCPOS($cPOS)
{
    $this->CPOS = $cPOS;
}

/**
 * Get CPOS
 *
 * @return string 
 */
public function getCPOS()
{
    return $this->CPOS;
}

/**
 * Set LCOM
 *
 * @param string $lCOM
 */
public function setLCOM($lCOM)
{
    $this->LCOM = $lCOM;
}

/**
 * Get LCOM
 *
 * @return string 
 */
public function getLCOM()
{
    return $this->LCOM;
}

/**
 * Set INSEE
 *
 * @param string $iNSEE
 */
public function setINSEE($iNSEE)
{
    $this->INSEE = $iNSEE;
}

/**
 * Get INSEE
 *
 * @return string 
 */
public function getINSEE()
{
    return $this->INSEE;
}

/**
 * Set DEP
 *
 * @param string $dEP
 */
public function setDEP($dEP)
{
    if (!isset($this->DEP))
        $this->DEP = '02';
    $this->DEP = $dEP;
}

/**
 * Get DEP
 *
 * @return string 
 */
public function getDEP()
{
    if (!isset($this->DEP))
        $this->DEP = '02';
    return $this->DEP;
}

/**
 * Set ARR
 *
 * @param string $aRR
 */
public function setARR($aRR)
{
    $this->ARR = $aRR;
}

/**
 * Get ARR
 *
 * @return string 
 */
public function getARR()
{
    return $this->ARR;
}

/**
 * Set CAN
 *
 * @param string $cAN
 */
public function setCAN($cAN)
{
    $this->CAN = $cAN;
}

/**
 * Get CAN
 *
 * @return string 
 */
public function getCAN()
{
    return $this->CAN;
}

public function retrieveSiren($siret)
{
    return substr($siret, 0, 9);
}

public function retrieveNic($siret)
{
    return substr($siret, -5, 5);
}

//contraintes de validation
//TODO : valider les champs avec Regex

public function isSIREN()
{

}

public function isNIC()
{

}

}
like image 695
kzrdt Avatar asked Jun 20 '12 08:06

kzrdt


1 Answers

It's too late to reply here, but still replying so that it can help others who are stuck with the same problem.

Two things

  • UniqueConstraint is not needed unless you want to generate schema change queries from your entity.
  • UniqueEntity is what you need.

Short Answer

The entity definition in the question looks fine, I think there can be either of the below two issues.

  • You have added fields={"SIREN","NIC"} which means that they will together unique, but you might be wanting them to be unique individually
  • Or you might not be validating the entity, which I think is highly unlikely but I still wanted to be checked https://symfony.com/doc/current/validation.html#using-the-validator-service

Detailed Explanation below

Notice that I got rid of , uniqueConstraints={@ORM\UniqueConstraint(name="SIRET", columns={"SIREN", "NIC"})} from @ORM/Table annotation (because UniqueConstraint is not needed unless you want to generate schema definition out of the entity)

<?php

namespace Proetco\FrontBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * Proetco\FrontBundle\Entity\Entreprise
 *
 * @ORM\Table(name="entreprise") 
 * @ORM\Entity(repositoryClass="Proetco\FrontBundle\Entity\EntrepriseRepository")
 * @UniqueEntity(fields={"SIREN","NIC"}, message="Cette entreprise est déjà enregistrée")
 */
class Entreprise {
    /**
     * Body of entity
     */
}

Also fields={"SIREN","NIC"} this means that you want both these columns together to be unique If you want each one of them to be inidividually unique then you'll need two UniqueEntity

Something like below

/**
 * @ORM\Table(name="entreprise") 
 * @ORM\Entity(repositoryClass="Proetco\FrontBundle\Entity\EntrepriseRepository")
 * @UniqueEntity(fields={"SIREN"}, message="Cette entreprise est déjà enregistrée")
 * @UniqueEntity(fields={"NIC"}, message="Cette entreprise est déjà enregistrée")
 */

After you have the above setup you'll need validate entity and handle errors in your form

like image 111
Anujith Singh Avatar answered Sep 25 '22 01:09

Anujith Singh