Php 一个实体上有多个@uniquentity注释
我有一个实体,它有两个唯一的索引。出于验证目的,我想向实体添加两个约束 给定实体:Php 一个实体上有多个@uniquentity注释,php,symfony,doctrine-orm,constraints,Php,Symfony,Doctrine Orm,Constraints,我有一个实体,它有两个唯一的索引。出于验证目的,我想向实体添加两个约束 给定实体: /** * @ORM\Entity * @ORM\Table(name="sample", uniqueConstraints={ * @ORM\UniqueConstraint(columns={"user_id", "hash"}), * @ORM\UniqueConstraint(columns={"user_id", "name"}) * }) * @UniqueEntity(
/**
* @ORM\Entity
* @ORM\Table(name="sample", uniqueConstraints={
* @ORM\UniqueConstraint(columns={"user_id", "hash"}),
* @ORM\UniqueConstraint(columns={"user_id", "name"})
* })
* @UniqueEntity(fields={"user", "jobSearch"}, message="duplicate_hash")
* @UniqueEntity(fields={"user", "name"}, message="duplicate_name")
*/
class SampleEntity
{
// ...
}
问题是:只有最后的@uniquentity
得到尊重。因此,如果切换Uniquentity注释,则只有user
和name
的重复键才能被识别,反之亦然,只有user
和jobSearch
的重复键才能被识别
是否有任何解决方案可以使用
@uniquentity
覆盖两个唯一的索引?我通过使用自定义验证解决了这个问题。样本代码在src/AppBundle/Validator/Constraints中 实体类样本
<?php // src/AppBundle/Enitiy/Campaign.php
namespace AppBundle\Entity;
use AppBundle\Validator\Constraints as AppAssert;
use Doctrine\ORM\Mapping as ORM,
Doctrine\Common\Collections\ArrayCollection;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity,
Symfony\Component\Validator\Constraints as Assert;
use \DateTime;
/**
* Campaign
*
* @ORM\Table(name="campaign")
* @ORM\Entity(repositoryClass="AppBundle\Entity\CampaignRepository")
* @UniqueEntity(fields={"code", "name"})
*/
class Campaign
{
// other stuff
/**
* @var string
*
* @ORM\Column(name="code", type="string", length=255, unique=true)
*/
private $code;
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=255, unique=true)
*/
private $name;
/**
* Get code
*
* @Assert\Length(min=2, max=11, groups={"create", "update"})
* @Assert\Regex(pattern="/\d/", match=true, message="Code must be numeric", groups={"create", "update"})
* @Assert\NotBlank(groups={"create", "update"})
* @AppAssert\UniqueCodeName(groups={"create", "update"})
* @return string
*/
public function getCode()
{
return $this->code;
}
}
我想创建一个链式验证器
,它可以容纳n个其他验证器:
/**
* @ORM\Entity
* @ORM\Table(name="sample", uniqueConstraints={
* @ORM\UniqueConstraint(columns={"user_id", "hash"}),
* @ORM\UniqueConstraint(columns={"user_id", "name"})
* })
* @Chain(constraints={
* @UniqueEntity(fields={"user", "hash"}, message="duplicate_hash"),
* @UniqueEntity(fields={"user", "name"}, message="duplicate_name")
* })
*/
class SampleEntity
{
// ...
}
这样就可以将nuniquentity
验证器添加到一个实体中
当我在谷歌上搜索,看看是否有人已经有了这个想法。我发现了这个要点:这基本上正是我所需要的,并且最终以一种非常相似的方式实现。我也遇到了同样的问题,并最终编写了一个自定义验证来防止表单提交,如果这对您有用,我可以发布一些code@Rooneyl是的,这对我来说绝对有效。。。如果您能为您的代码-)共享您的发现/code.Thx,那就太好了。它启发我写了一个ChainValidator
,它可以容纳n个其他验证器。这样就可以添加多个UniquentityValidator。我将在大约一分钟后发布代码
services:
validator.unique.campaign_code_name:
class: AppBundle\Validator\Constraints\UniqueCodeNameValidator
tags:
- { name: validator.constraint_validator, alias: code_name }
arguments: ["@doctrine.orm.entity_manager"]
<?php // src/AppBundle/Enitiy/Campaign.php
namespace AppBundle\Entity;
use AppBundle\Validator\Constraints as AppAssert;
use Doctrine\ORM\Mapping as ORM,
Doctrine\Common\Collections\ArrayCollection;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity,
Symfony\Component\Validator\Constraints as Assert;
use \DateTime;
/**
* Campaign
*
* @ORM\Table(name="campaign")
* @ORM\Entity(repositoryClass="AppBundle\Entity\CampaignRepository")
* @UniqueEntity(fields={"code", "name"})
*/
class Campaign
{
// other stuff
/**
* @var string
*
* @ORM\Column(name="code", type="string", length=255, unique=true)
*/
private $code;
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=255, unique=true)
*/
private $name;
/**
* Get code
*
* @Assert\Length(min=2, max=11, groups={"create", "update"})
* @Assert\Regex(pattern="/\d/", match=true, message="Code must be numeric", groups={"create", "update"})
* @Assert\NotBlank(groups={"create", "update"})
* @AppAssert\UniqueCodeName(groups={"create", "update"})
* @return string
*/
public function getCode()
{
return $this->code;
}
}
/**
* @ORM\Entity
* @ORM\Table(name="sample", uniqueConstraints={
* @ORM\UniqueConstraint(columns={"user_id", "hash"}),
* @ORM\UniqueConstraint(columns={"user_id", "name"})
* })
* @Chain(constraints={
* @UniqueEntity(fields={"user", "hash"}, message="duplicate_hash"),
* @UniqueEntity(fields={"user", "name"}, message="duplicate_name")
* })
*/
class SampleEntity
{
// ...
}