gpt4 book ai didi

Symfony2 Doctrine2 UniqueEntity 验证指定一个值

转载 作者:行者123 更新时间:2023-12-04 00:09:37 30 4
gpt4 key购买 nike

我已经在时尚领域使用了 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 = 37status = 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
}

最佳答案

您可以添加 repositoryMethod UniqueEntity 约束注释的选项。

* @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 [];
}

关于Symfony2 Doctrine2 UniqueEntity 验证指定一个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38039711/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com