有这样一个产品实体:
class Product
{
private $itemNumber;
.
.
.
但在某些情况下,我想推翻这种“独特”的行为。
我需要在表单级别上进行此重写。在数据库级别,应该保留唯一的行为。我想通过一个表单事件来实现这一点,文章编号得到一个前缀。类似这样:
class ProductCreateListener
{
public function prePersist(LifecycleEventArgs $eventArgs)
{
$entity = $eventArgs->getObject();
if (!$entity instanceof Product) {
return;
}
if (!$entity->getCategories() instanceof ArrayCollection) {
return;
}
if (isset($entity->getCategories()[0])) {
$firstCat = $entity->getCategories()[0];
if ($firstCat->getNameSlug() == 'my_nameslug') {
$entity->setItemNumber('my_prefix_'.$entity->getItemNumber());
}
}
}
}
因此,如果我的产品有特定的类别,itemnumber将在数据库级别获得前缀。
问题是,我的事件将在实体断言后激发:
@DoctrineAssert\Uniquentity(“项目编号”)
如何更改我的事件先更改文章编号,然后在表单中检查实体断言?