【发布时间】:2011-05-21 15:51:13
【问题描述】:
我的验证有问题。在教义 1 中,我使用了这个:
if ($model->isValid()) {
$model->save();
} else {
$errorStack = $model->getErrorStack();
...
}
在 $errorStack 中,我得到了列名和错误消息。但在 Doctrine 2 中,我可以只使用它:
实体
/**
* @PrePersist @PreUpdate
*/
public function validate()
{
if ($this->name == null)) {
throw new \Exception("Name can't be null");
}
}
控制器:
try {
$user = new \User();
//$user->setName('name');
$user->setCity('London');
$this->_entityManager->persist($user);
$this->_entityManager->flush();
} catch(Exception $e) {
error_log($e->getMessage());
}
但我有两个问题:
- 我不知道是哪一栏?
- 我不想手动检查唯一性
如果我从实体中跳过 validate(),则将捕获唯一性(来自此 error.log)
Unique violation: 7 ERROR: duplicate key value violates unique constraint "person_email_uniq"
但例如用户保存了 2 条记录,第一条是错误的,但第二条有效,第一次保存后 EntityManager 将关闭,我无法保存第二条(好)记录,因为“EntityManager 已关闭” .
这个问题的最佳解决方案是什么?
【问题讨论】:
标签: php validation unique doctrine-orm