zend-framework - 使用连接在 Zend Select 中设置完整性检查

标签 zend-framework zend-db-select

我在看 some questions询问如何在 Zend Framework 查询中进行连接,但答案总是类似于“只做 setIntegrityCheck(FALSE)”。

我的问题是:为什么我需要这样做?

在我看来,禁用“完整性检查”并不是进行这项工作的正确方法。在我的特殊情况下,我使用 MySQL 数据库和一些带有外键的 InnoDB 表,例如:

CREATE TABLE IF NOT EXISTS `tableA`
(
`id` CHAR(6),
`name` VARCHAR(255),
PRIMARY KEY (`id`)
) ENGINE=InnoDB;

CREATE TABLE IF NOT EXISTS `tableB`
(
`tableA_id` CHAR(6),
`somefield` VARCHAR(255),
PRIMARY KEY (`tableA_id`)
) ENGINE=InnoDB;

ALTER TABLE `tableB` ADD FOREIGN KEY fk1 (`tableA_id`) REFERENCES `tableA` (`id`);

(这是我的数据库的一个非常简化的版本)

而且,我的查询代码如下所示:
$table = new Zend_Db_Table('tableB');
$select = $table->select(TRUE)
  ->join(array('a' => 'tableA'), 'tableB.tableA_id = a.id');
$result = $table->fetchAll($select);

除非我添加 setIntegrity(FALSE),否则这给了我“选择查询无法与另一个表连接”异常。到我的 $select .

最佳答案

调用 setIntegrityCheck(false)是进行连接的正确方法;如果您正在使用 Zend_Db_TableZend_Db_Table_Select ,除非禁用完整性检查,否则无法加入。

完整性检查只是为了确保查询不使用多个表,并且在适当时确保Zend_Db_Table_Row对象可以被删除或修改然后保存,因为行数据是单个表独有的,而不是来自不同表的数据的混合。

要表明您想使用多个表,请指定 setIntegrityCheck(false)让 Zend Framework 知道这是故意的。结果是您得到一个无法调用 save() 的锁定行。或 delete()在。

这是 Zend_Db_Table - Advanced Usage 上引用指南的引述(跳到示例 27。

The Zend_Db_Table_Select is primarily used to constrain and validate so that it may enforce the criteria for a legal SELECT query. However there may be certain cases where you require the flexibility of the Zend_Db_Table_Row component and do not require a writable or deletable row. for this specific user case, it is possible to retrieve a row or rowset by passing a FALSE value to setIntegrityCheck(). The resulting row or rowset will be returned as a 'locked' row (meaning the save(), delete() and any field-setting methods will throw an exception).



另见:One-to-Many Joins with Zend_Db_Table_Select

关于zend-framework - 使用连接在 Zend Select 中设置完整性检查,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9440140/

相关文章:

php - 发送 : How to insert NULL values into MySQL

php - 如何使用 php 运行具有多个创建命令的 sql 文件,而无需手动解析 sql 文件?

php - 构造函数参数在 init 中不可用

mysql - zf2 db sql where datetime before two ours ago

mysql - 计算大型 mysql 查询中的行数 (zend)

zend-framework - Zend Framework 2 Doctrine ORM 身份验证

php - 在 Zend_Form 类中的 addElement() 上使用 Between 验证器

php - MySQL 查询 - 选择所有帖子并计算每个帖子的评论

php - Zend DB 无法模拟 MS SQL 的限制和偏移量

php - zend 框架选择对象和复杂连接