我已经在时尚中使用了UniqueEntity,如:
/**
* @ORM\Entity()
* @ORM\Table(name="benefit_group_category")
* @UniqueEntity(fields={"name", "project"}, ignoreNull=false, message="Duplicated group category for this project")
*/
所以我已经知道如何使用它了。
我在另一个背景下需要做的是:
* @UniqueEntity(fields={"entityId", "status"}, message="There is already a Quality Review Pending for this entity")
但是我需要指定相同的entityId
我不能重复status == 1
,但如果status == 0
它应该通过验证。
基本上,例如,我可以使用entityId = 37
和status = 0
创建任意数量的记录,但只有status = 1
的记录。
这可能吗?
编辑:请在此处找到它的更新版本,以便进行双重检查。在我原来的问题中,我忘了提到另一个参数(entityName),但它没有改变它的本质。
存储库类
<?php
namespace AppBundle\Entity;
/**
* QualityReviewRequestRepository
*
* This class was generated by the Doctrine ORM. Add your own custom
* repository methods below.
*/
class QualityReviewRequestRepository extends \Doctrine\ORM\EntityRepository
{
/**
* @param array $criteria
* @return array
*/
public function getByEntityIdAndEntityNameAndStatusCriteria(array $criteria)
{
// Todo remove this first return, it's done on purpose to force the system to throw an error, but it doesn't.
return $this->createQueryBuilder('e')
->andWhere('e.entityId = :entityId')
->setParameter('entityId', $criteria['entityId'])
->getQuery()
->getResult()
;
if (QualityReviewRequest::STATUS_NEW == $criteria['status']) {
return $this->createQueryBuilder('e')
->andWhere('e.entityId = :entityId')
->setParameter('entityId', $criteria['entityId'])
->andWhere('e.entityName = :entityName')
->setParameter('entityName', $criteria['entityName'])
->andWhere('e.status = :status')
->setParameter('status', $criteria['status'])
->getQuery()
->getResult()
;
}
return [];
}
}
主要课程:
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Timestampable\Traits\TimestampableEntity;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
/**
* QualityReviewRequest
*
* @ORM\Table(name="quality_review_requests")
* @ORM\Entity(repositoryClass="AppBundle\Entity\QualityReviewRequestRepository")
* @UniqueEntity(fields={"entityId","entityName","status"}, repositoryMethod="getByEntityIdAndEntityNameAndStatusCriteria", message="There is already a Quality Review Pending for this entity")
*/
class QualityReviewRequest
{
const STATUS_NEW = 0;
const STATUS_DONE = 1;
use TimestampableEntity;
// removed useless stuff
/**
* @var string
*
* @ORM\Column(name="entityName", type="string", length=255)
*/
private $entityName;
/**
* @var integer
*
* @ORM\Column(name="entityId", type="integer")
*/
private $entityId;
// More stuff
}
答案 0 :(得分:3)
您可以向UniqueEntity约束注释添加repositoryMethod选项。
* @UniqueEntity(fields={"entityId", "status"}, repositoryMethod="getEntityByIdAndStatusCriteria", message="There is already a Quality Review Pending for this entity")
并实施存储库方法以按定义的条件查找实体。
/**
* @param array $criteria
* @return array
*/
public function getEntityByIdAndStatusCriteria(array $criteria)
{
if (1 == $criteria['status']) {
return $this->createQueryBuilder('e')
->andWhere('e.entityId = :entityId')
->setParameter('entityId', $criteria['entityId'])
->andWhere('e.status = :status')
->setParameter('status', $criteria['status'])
->getQuery()
->getResult()
;
}
return [];
}