如何在 Kohana 3.2 中获取 Database_Exception [1062](“重复条目”)的主键?

发布于 2024-12-15 10:03:50 字数 545 浏览 3 评论 0原文

假设我有一个表,其中包含名为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

赢得她心 2024-12-22 10:03:50

不,这是不可能的。

预计这也不会具有来自异常的重复条目的主键

  • 抛出此异常的是底层数据库。
  • 如果数据库能够以某种方式报告该消息,则这是可能的。但这会导致很多类似的案例,需要更多的信息。我相信所有数据库供应商都需要确认这一点,这是不可能的。

NO, this is not posible.

This is not expected also to have the primary key for that duplicate entry from a exception.

  • It is underlying database which throw this exception.
  • It could be possible if database could report that message somehow. But it will lead to so many similar cases where much more info is needed. And all the database vendor needs to confirm this which is impossible, I believe.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文