我正在使用Gedmo\Mapping\Annotation\Slug
为Symfony实体生成一个子弹。此slug属性定义为唯一:
/**
* @Gedmo\Slug(fields={"type"})
* @ORM\Column(type="string", length=255, unique=true)
*/
private $slug;
我希望能够在持久化之前检索生成的子弹字符串,以便正确处理统一性错误。
我当然可以捕获persist()
方法引发的异常,但是由于其他原因,它不是一个好的解决方案。
为简洁起见,我从文档中举了一个例子,并添加了我需要的东西(使用伪代码):
<?php
$article = new Article();
$article->setTitle('the title');
$article->setCode('my code');
// What I need is the 'getUpdatedSlugName()' function:
if (getUpdatedSlugName($article) [already exists]) {
throw new MyException("Sorry, this article name/slug already exists!");
}
$this->em->persist($article);
$this->em->flush();