我有一个SQLite表,其中包含有关图像的数据。一列是文本字段,其中包含该图像在网站上的显示位置。该字段具有唯一的约束。以下是表定义:position
CREATE TABLE images (id INTEGER PRIMARY KEY, title TEXT UNIQUE NOT NULL, position TEXT UNIQUE
NOT NULL, caption TEXT, filename TEXT, album INTEGER NOT NULL, publicationDate TEXT, updated
TEXT, createdBy INTEGER
NOT NULL, updatedBy INTEGER NOT NULL, FOREIGN KEY (createdBy) REFERENCES users(id), FOREIGN KEY
(album) REFERENCES albums(id), FOREIGN KEY (updatedBy) REFERENCES users(id));
并且,来自实体的相关位:
/**
* @ORM\Entity
* @ORM\Table(name="images",indexes={
* @Index(name="publication_date", columns={"publicationDate"})},
* uniqueConstraints={@UniqueConstraint(name="unique_position", columns={"position", "fileName"})}
* )
*/
class Image
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue
**/
protected $id;
/**
* @ORM\Column(type="string", unique=true)
*/
protected $position;
我可以成功地写入表(Doctrine 保留,然后刷新),除非列上有唯一的约束冲突。当存在唯一的约束冲突时,flush() 将以静默方式失败(不写入数据库),并且应用程序将继续执行。以下是相关代码 - 设置并保留实体后:position
try {
$this->entityManager->flush();
$message['type'] = 'alert-info';
$message['content'] = "$title added succesfully";
return $message;
} catch (UniqueConstraintViolationException $e) {
$message['type'] = 'alert-danger';
$message['content'] = "$title could not be added " . $e;
return $this->create($message, $formVars);
} catch (Exception $e) {
$message['type'] = 'alert-danger';
$message['content'] = "$title could not be added " . $e;
return $this->create($message, $formVars);
}
为什么我没有捕捉到异常?或者,它根本没有被扔掉吗?
呼唤远方