Error handling Doctrine2 ORM EntityManager获取发生的错误
我有一个名为Theme的实体,其名称不是Id,但应编制索引且唯一:Error handling Doctrine2 ORM EntityManager获取发生的错误,error-handling,doctrine-orm,entitymanager,flush,Error Handling,Doctrine Orm,Entitymanager,Flush,我有一个名为Theme的实体,其名称不是Id,但应编制索引且唯一: /** * @Entity @Table(name="themes", * uniqueConstraints={@UniqueConstraint(name="theme_unique",columns={"name"})}, * indexes={@Index(name="theme_idx", columns={"name"})}) */ class Th
/**
* @Entity @Table(name="themes",
* uniqueConstraints={@UniqueConstraint(name="theme_unique",columns={"name"})},
* indexes={@Index(name="theme_idx", columns={"name"})})
*/
class Theme {...}
我使用现有名称创建一个新主题。这将传递一个错误,脚本将停止。
如何获取该错误并继续脚本
function CreateTheme($newThemeName) {
//$theme = $this->FindByThemeName ( $newThemeName );
//if (! $theme) {
$theme = new Theme ();
$theme->setName ( $newThemeName );
$theme->setIsActive ( false );
$theme->setIsDefault ( false );
$this->entityManager->persist ( $theme );
$this->entityManager->flush ();
return $theme;
//} else {
// $this->error = "Error in flush was avoided (name not being unique)!";
// return null;
//}
Try Catch不起作用,这就是这个问题的原因 ORMException的处理程序不知何故未注册,现在已注册。不清楚是什么引起的
function CreateTheme($themeName) {
global $entityManager;
$theme = new Theme ();
$theme->setName ( $themeName );
$theme->setIsActive ( false );
$theme->setIsDefault ( false );
try {
$entityManager->persist ( $theme );
$entityManager->flush ();
} catch (Exception $e) {
echo $e->getMessage();
$theme = null;
}
return $theme;
}
使用重复名称时,给出:
SQLSTATE[23000]:完整性约束冲突:键“theme_unique”的1062重复项“test”
try/catch设置并不能真正解决问题: 由于SQL错误,条令\ORM\EntityManager已关闭。 EntityManager不能简单地重置(或重新打开)。有关此主题的链接: 声明:“最好避免使用关闭的实体管理器(这意味着在将数据发送到数据库之前验证数据)” 结论:设置UniqueConstraints的注释是无用的:它不会阻止执行语法有效的SQL指令,因为该指令会由于该约束而导致“重复条目”SQL错误。 此错误关闭无法安全重置的EntityManager。。。。 在尝试持久化和刷新新实体实例之前,需要“手动”检查uniqueconstraints的vioalation