1

让我们考虑一个非常基本的表格:

CREATE TABLE test_warning (col_a INT NOT NULL, col_b INT NOT NULL)

+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| col_a | int(11) | NO   |     | NULL    |       |
| col_b | int(11) | NO   |     | NULL    |       |
+-------+---------+------+-----+---------+-------+

当我插入未指定其中一列的行时,会生成警告:

INSERT INTO test_warning (col_a) VALUES (1);
Query OK, 1 row affected, 1 warning (0.05 sec)

SHOW WARNINGS;
+---------+------+--------------------------------------------+
| Level   | Code | Message                                    |
+---------+------+--------------------------------------------+
| Warning | 1364 | Field 'col_b' doesn't have a default value |
+---------+------+--------------------------------------------+

有没有人试图检索在 zend 应用程序中SHOW WARNINGS使用的输出?Zend_Db_Adapter

我试过以下:

  • 插入带有适配器的行(例如,通过创建Zend_Db_Row并保存它)缺少列的值以引发警告。
  • 执行类似的命令$adapter->query('SHOW WARNINGS')->fetchAll();。这将返回一个空数组。

我开始相信这Zend_Db_Adapter“仅”对通过SELECT.

4

1 回答 1

1

有趣的问题。我刚刚运行了一个测试, fetchAll()似乎确实返回了 MySQL 警告的结果,至少对我来说是这样!我正在运行 ZF 1.12.0

首先,我在命令行上运行了一条 SQL 语句,其中字段的值超过了 VARCHAR 限制,并且列没有默认值。

结果如下:

mysql> INSERT INTO `myTable` (`example2`) VALUES ('a value that is too long');

mysql> show warnings;
+---------+------+-------------------------------------------------+
| Level   | Code | Message                                         |
+---------+------+-------------------------------------------------+
| Warning | 1364 | Field 'example1' doesn't have a default value   |
| Warning | 1265 | Data truncated for column 'example2' at row 1   |
+---------+------+-------------------------------------------------+

然后我使用 Zend_Db 运行相同的查询...

$stmt = $db->query(
    'INSERT INTO `myTable` (`example2`) VALUES (\'a value that is too long\')'
);


$stmt = $db->query(
    'SHOW WARNINGS;'
);

$result = $stmt->fetchAll();

echo '<pre>';
var_dump($result);
echo '</pre>';

结果如下:

array(4) {
  [0]=>
  array(3) {
    ["Level"]=>
    string(7) "Warning"
    ["Code"]=>
    string(4) "1364"
    ["Message"]=>
    string(44) "Field 'example1' doesn't have a default value"
  }
  [1]=>
  array(3) {
    ["Level"]=>
    string(7) "Warning"
    ["Code"]=>
    string(4) "1265"
    ["Message"]=>
    string(44) "Data truncated for column 'example2' at row 1"
  }
}

这里的主要区别是我将 SQL 直接写入 query() 方法,而不是使用 Zend_db_Select 构造它。值得一试,尽管理论上它不应该有所作为。

于 2013-04-01T21:54:04.300 回答