PHP 版本之间的不同 PDO 行为

Different PDO behaviours between PHP versions

我有三台服务器 运行 作为我的开发、暂存和生产服务器。 PHP版本运行分别为:5.5.235.5.95.5.20

它们是运行完全相同的代码。但是我的开发服务器抛出异常,而其他两个工作正常。例外情况如下:

Error: [PDOException] SQLSTATE[HY000]: General error: 1366 Incorrect integer value: '' for column 'comment_id' at row 1
Request URL: /comments/add.json Stack Trace: 0 /Users/me/Sites/MySite/lib/Cake/Model/Datasource/DboSource.php(460): PDOStatement->execute(Array)1
1 /Users/me/Sites/MySite/lib/Cake/Model/Datasource/DboSource.php(426): DboSource->_execute('INSERT INTO `po...', Array)1
2 /Users/me/Sites/MySite/lib/Cake/Model/Datasource/DboSource.php(1008): DboSource->execute('INSERT INTO `po...')1
3 /Users/me/Sites/MySite/lib/Cake/Model/Model.php(1744): DboSource->create(Object(Notification), Array, Array)1
4 /Users/me/Sites/MySite/app/Controller/CommentsController.php(181): Model->save(Array)1
5 [internal function]: CommentsController->add()

插入的值是 bool(false),而数据库需要整数。所以我不得不为我的开发服务器做一些事情来处理这个问题:

//The comment id is set to false. Needs to turn it to NULL as DB can't handle false for integer
if($this->Comment->id === false)
    $notification['Notification']['comment_id'] = NULL;
else
    $notification['Notification']['comment_id'] = $this->Comment->id;

我的问题是:为什么我的暂存和生产服务器可以处理这个问题(可能会自动将假值转换为整数),但我的开发服务器不能?

如您所见,我的服务器是 运行 CakePHP。我一直在查看 PDO 版本,但它似乎并没有给我太多见识。我的开发服务器是 运行 在我的 Mac Book Mac OS X 10.10 上。分期和生产是 运行 Ubuntu 12.04.

您很可能在生产和暂存环境中禁用了 MySQL 的严格模式,而您的开发环境启用了此模式。

See more here:

Strict mode controls how MySQL handles invalid or missing values in data-change statements such as INSERT or UPDATE. A value can be invalid for several reasons. For example, it might have the wrong data type for the column, or it might be out of range. A value is missing when a new row to be inserted does not contain a value for a non-NULL column that has no explicit DEFAULT clause in its definition. (For a NULL column, NULL is inserted if the value is missing.)

If strict mode is not in effect, MySQL inserts adjusted values for invalid or missing values and produces warnings

您可以继续阅读 how to change this mode here

在 OS X 上,following link has more details:

After a lot of poking around I found out that MySQL for OS X from Oracle ships with a /usr/local/mysql/my.cnf which is loaded on startup. In this file is a sole configuration directive for sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES. Once I commented this out and restarted the server strict mode was off, my ORM worked and I was happy.