PHP PDO lastInsertId 返回字符串而不是整数

发布于 2024-09-30 16:48:49 字数 1166 浏览 7 评论 0原文

我目前正在构建一个 symfony/doctrine 应用程序,一个模型具有以下结构:

Session:
  columns:
    session_id:
      type: integer(8)
      primary: true
      autoincrement: true
    session_number:
      type: string(30)

这正确映射到数据库为:

CREATE TABLE `session` (
  `session_id` bigint(20) NOT NULL AUTO_INCREMENT,
  `session_number` varchar(30) NOT NULL,

所以我们在那里是安全的。

然后,当尝试创建新记录时,代码会这样:

$newSession = new Session();
$newSession->session_number = session_id();
$newSession->save();
//...
echo $newSession->session_id; //<------ this will return a string '1'

最后一行是有问题的。 session_id字段被填充为最后一个身份值,但是作为字符串!!

所以一开始我认为教义是罪魁祸首,但是后来调试代码,我发现似乎PDO是实际上是将值作为字符串返回的。被调用的方法是:

dbh->lastInsertId()

所以明显的问题是:我该如何解决这个问题?。我在互联网上找到了这些东西,但还没有尝试过:

  1. PDO::ATTR_STRINGIFY_FETCHES。这行得通吗?如果是,我该如何配置?
  2. 使用 mysql 本机驱动程序而不是 pdo。然而,教义处理它,所以我不确定我是否可以配置它。
  3. 另一个明显的解决方法是类型转换为 int : (int)$session->session_id。但这实在是丑陋,因为它应该返回一个整数值(这就是使用 pdo 和学说的全部目的!)并且所有其他模型都会有同样的问题。
  4. 对此最好的解决方案是什么?

I'm currently building a symfony/doctrine app and one model has the following structure:

Session:
  columns:
    session_id:
      type: integer(8)
      primary: true
      autoincrement: true
    session_number:
      type: string(30)

This correctly maps to the database as:

CREATE TABLE `session` (
  `session_id` bigint(20) NOT NULL AUTO_INCREMENT,
  `session_number` varchar(30) NOT NULL,

So we are safe there.

Then when trying to create a new record the code goes:

$newSession = new Session();
$newSession->session_number = session_id();
$newSession->save();
//...
echo $newSession->session_id; //<------ this will return a string '1'

The last line is the problematic. The session_id field gets filled in with the last identity value but as a string!!

So at first, I thought doctrine was the culprit, but then debugging the code, I found out that it seems that PDO is actually the one that is returning the value as a string. The method that gets called is:

dbh->lastInsertId()

So the obvious question is: how can I solve this?. I found these things in the internet but haven't tried them yet:

  1. PDO::ATTR_STRINGIFY_FETCHES. Will this work? If yes, how can I configure that?
  2. Using mysql native driver instead of pdo. However doctrine handles it so I'm not sure if I can even configure that.
  3. The other obvious workaround is to typecast to int : (int)$session->session_id. But that's just plain ugly as it should return an integer value (that's the whole purpose of using pdo and doctrine!) and all the other models would have the same issue.
  4. What would be the best solution for this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

转角预定愛 2024-10-07 16:48:49

您可能想使用转换函数参见php手册

$myInteger = intval($myString)

为了在字符串表示的数字太大而无法转换为整数时发送异常(并避免代码中出现一些令人讨厌的间歇性错误),请用以下代码括起来:

$myInteger = intval($myString)
if ($myInteger === PHP_INT_MAX) { // PHP 4.4.0 & 5.0.5 min
      throw new Exception('Integer out of range, please call your developer for bug fixing!')
}

You may want to use conversion function see php manual.

$myInteger = intval($myString)

In order to send an exception if the string represents a too high number to be converted into an integer (and avoid some nasty intermittent bugs in your code), please surround with this code :

$myInteger = intval($myString)
if ($myInteger === PHP_INT_MAX) { // PHP 4.4.0 & 5.0.5 min
      throw new Exception('Integer out of range, please call your developer for bug fixing!')
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文