如何在 Kohana 3.2 中获取 Database_Exception [1062](“重复条目”)的主键?
假设我有一个表,其中包含名为 a
的列。它上面有索引UNIQUE KEY
。
在 ORM 模型中,我尝试插入到该表中。这是一种捕获当用户尝试在列 a
中插入不唯一的内容时发生的 Database_Exception [ 1062 ]
的方法:
function save(Validation $validation = null) {
try {
parent::save($validation);
}
catch (Database_Exception $exception) {
if ($exception->getCode() === 1062) {
// PK?
}
}
}
现在我正在尝试获取条目的主键已经有我试图复制的内容。是否可以不再进行 SQL 查询?我希望该行的主键返回到某处。
很抱歉胡言乱语,但这有点难以解释。谢谢指教!
Lets say that I have a table with column called a
. It has index UNIQUE KEY
on it.
In ORM model, I try to insert into that table. This is a way to catch Database_Exception [ 1062 ]
that occurs when user tries to insert something in column a
that's not unique:
function save(Validation $validation = null) {
try {
parent::save($validation);
}
catch (Database_Exception $exception) {
if ($exception->getCode() === 1062) {
// PK?
}
}
}
Now I'm trying to get primary key of entry that already have that content what I tried to duplicate. Is it possible without any more SQL queries? I hope that primary key of that row is returned somewhere.
Sorry about non-sense, but it was kinda hard to explain. Thanks in an advice!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不,这是不可能的。
预计这也不会具有来自异常的重复条目的
主键
。NO, this is not posible.
This is not expected also to have the
primary key
for that duplicate entry from a exception.