Symfony2 UniqueConstraint and UniqueEntity

I have a little problem with the Doctrine entity for several days. After that, I defined it using UniqueConstraint in two fields with a UniqueEntity Validation check. But after checking my form with the addition of an object already in the database, there is no way to get 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 declaration of essence, everything seems fine to me, but perhaps I forgot 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()
{

}

}
+5
source share
3 answers

, , , .


, , .

  • fields={"SIREN","NIC"}, , , .
  • , , , , https://symfony.com/doc/current/validation.html#using-the-validator-service

, , uniqueConstraints={@ORM\UniqueConstraint(name="SIRET", columns={"SIREN", "NIC"})} @ORM/Table ( UniqueConstraint , )

<?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
     */
}

fields={"SIREN","NIC"} , , , , UniqueEntity

-

/**
 * @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")
 */

, ,

+2

unique=true?
:

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

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

"unique = true" . . , , , . , -...

EDIT: ( ). loooooot , . , : https://groups.google.com/forum/#!topic/symfony2/veb6Leph2_w

So, I used the same solution: try / catch. I just have to assume that the only exception thrown by the database is a unique violation of restrictions ...

// ...
try {
 $this->persistAndFlush($user);

 return $this->redirect($this->generateUrl('publish'));
}
catch (\PDOException $e) {
 $error = $this->trans('user.username.notUnique',array(),'validators');
}
// ...

This is not satisfactory, but it works. And I have lost enough time with this for today.

Reply to vegemite4me: Sorry, but I am not familiar with Stackoverflow. I did not find the comment button, and now I see the only one I can answer. But it doesn’t work ... Either I’m out of luck, or I’m noob, or the form is very poorly made. Probably three of them

-1
source

All Articles