Symfony约束,以防止在重叠的时间帧中出现重复



如何使用Doctrine ORM在Symfony中的重叠日期范围内强制值的唯一性。

我有以下实体

<?php
/**
* @ORMEntity
* @ORMTable("tax_component")
*/
class TaxComponent
{
/**
* @ORMId()
* @ORMGeneratedValue()
* @ORMColumn(name="tax_component_id", type="integer")
*/
private ?int $id;
/**
* @ORMColumn(name="tax_component_name", type="string", length=20)
*/
private string $name;
/**
* @ORMColumn(name="tax_component_rate", type="integer")
* @AssertGreaterThanOrEqual(0)
*/
private int $rate;
/**
* @ORMColumn(name="tax_component_applicable_from", type="datetime_immutable")
*/
private DateTimeInterface $applicableFrom;
/**
* @ORMColumn(name="tax_component_applicable_to", type="datetime_immutable")
*/
public function __construct(string $name, int $rate, ?DateTimeImmutable $applicableFrom = null, ?DateTimeImmutable $applicableTo = null)
{
...
}
}

我想使$name$applicableFrom$applicableTo的重叠时间帧内是唯一的。例如,

$repository->save(
new TaxComponent('inter-state', 1800, new DateTime('2018-04-01:00:00:00'), new DateTime('2019-03-31T23:59:59'))
);
// The following should be allowed since there is no overlap between the two time ranges using the name 'inter-state'
$repository->save(
new TaxComponent('inter-state', 1200, new DateTime('2019-04-01:00:00:00'), new DateTime('2020-03-31T23:59:59'))
);
// The following should fail since 'inter-state' is ambiguous during the period 2019-09-01:00:00:00 to 2020-03-31T23:59:59
$repository->save(
new TaxComponent('inter-state', 1800, new DateTime('2019-09-01:00:00:00'), new DateTime('2020-09-31T23:59:59'))
);

Symfony是否有强制执行的约束?

我正计划在调用$this->entityManager->persist之前,从TaxComponentRepository::save中检查现有实体。有更好的解决方案吗?

更干净的方法是创建自己的自定义断言。

从创建约束开始:

<?php
namespace AppValidatorConstraints;
use SymfonyComponentValidatorConstraint;
/**
* @Annotation
*/
class TaxComponentConstraint extends Constraint
{
public $message = 'Another tax component overlap this one: {{ taxComponent}}';
public function getTargets()
{
return self::CLASS_CONSTRAINT;
}
public function validatedBy()
{
return 'AppValidatorConstraintsTaxComponentValidator';
}
}

现在,您必须创建一个验证器,该验证器将检查是否存在与两个税务组件的重叠。

<?php
namespace AppValidatorConstraints;
use SymfonyComponentValidatorConstraint;
use SymfonyComponentValidatorConstraintValidator;
use SymfonyComponentValidatorExceptionUnexpectedTypeException;
class TaxComponentValidator extends ConstraintValidator
{
public function validate($taxComponentObject, Constraint $constraint)
{
//Check however you want if the tax component can be created (So no overlap between two existing TaxComponent)
if($overlappingTaxComponent){
$this->context->buildViolation($constraint->message)
->setParameter('{{ taxComponent }}', $overlappingTaxComponent->__toString())
->addViolation();
}
}
}

这里,$overlapingTaxComponent是一个TaxComponent,由于您的限制,我们无法制作它。

如果约束正确完成,您现在可以在实体中轻松使用它,以便在提交表单时自动检查:

<?php
//...
use AppValidatorConstraints as CustomAssert;
/**
* @ORMEntity
* @ORMTable("tax_component")
* @CustomAssertTaxComponentConstraint
*/
class TaxComponent
{
/**
* @ORMId()
* @ORMGeneratedValue()
* @ORMColumn(name="tax_component_id", type="integer")
*/
private ?int $id;
/**
* @ORMColumn(name="tax_component_name", type="string", length=20)
*/
private string $name;

相关内容

  • 没有找到相关文章

最新更新