kzr*_*rdt 7 entity doctrine unique unique-constraint symfony
几天后我对Doctrine实体有一点问题.我在两个字段上使用UniqueConstraint定义了它,之后在这两个字段上进行了UniqueEntity验证.但是在通过添加已经在base中的实体来验证我的表单之后,无法在我的表单中获取我的错误消息.
只是来自Symfony的错误消息:
SQLSTATE [23000]:完整性约束违规:1062重复条目'339057986-00012'用于键'SIRET'
这是我的实体声明,对我来说似乎都很好,但也许我忘了或误解了什么?
<?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()
{
}
}
Run Code Online (Sandbox Code Playgroud)
小智 5
现在在这里回复已经为时已晚,但仍在回复,这样可以帮助陷入同样问题的其他人。
问题中的实体定义看起来不错,我认为可能存在以下两个问题之一。
fields={"SIREN","NIC"}这意味着它们在一起将是唯一的,但是您可能希望它们分别是唯一的https://symfony.com/doc/current/validation.html#using-the-validator-service请注意,我摆脱了注释(因为不需要UniqueConstraint,除非您, uniqueConstraints={@ORM\UniqueConstraint(name="SIRET", columns={"SIREN", "NIC"})}要从@ORM/Table实体中生成模式定义)
<?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
*/
}
Run Code Online (Sandbox Code Playgroud)
同样,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")
*/
Run Code Online (Sandbox Code Playgroud)
完成上述设置后,您需要验证实体并处理表单中的错误
小智 -1
为什么不放呢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;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8457 次 |
| 最近记录: |