使用 Zend 框架时从多个表获取数据?
是否有使用 Zend 从多个数据库表获取数据的最佳实践?我想知道而不是最终想要重构我在不久的将来编写的代码。我正在阅读 Zend 文档,它说:
“您不能指定 JOINed 表格将返回 行/行集。这样做会触发 PHP 错误。这样做是为了确保 Zend_Db_Table 的完整性是 保留。即 Zend_Db_Table_Row 应该只引用派生的列 来自其父表。”
我假设我需要使用多个模型 - 这是正确的吗?例如,如果我想获取特定用户 ID 的所有订单,其中日期位于两个日期之间,我该怎么办?
我知道可以从控制器访问两个不同的模型,然后在操作中组合它们各自的数据,但我不会高兴这样做,因为我一直在阅读 Survivalthedeepend.com 和 它告诉我我不应该'不要这样做...
在哪里、为什么以及如何:)
谢谢!
Is there a best practice in getting data from multiple database tables using Zend? I would like to know rather than end up wanting to refactor the code I write in the near future. I was reading the Zend documentation and it said that:
"You can not specify columns from a
JOINed tabled to be returned in a
row/rowset. Doing so will trigger a
PHP error. This was done to ensure
the integrity of the Zend_Db_Table is
retained. i.e. A Zend_Db_Table_Row
should only reference columns derived
from its parent table."
I assume I therefore need to use multiple models -- is that correct? If, for example, I want to get out all orders for a particular user id where the date is in between two dates what would I do?
I know that it would be possible to access the two different models from a controller and then combine their respective data in the action but I would not feel happy doing this since I have been reading survivethedeepend.com and it tells me that I shouldn't do this...
Where, why, and how? :)
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您正在阅读 ZFSTDE,请参阅第 9 章 (http://www.survivethedeepend.com/zendframeworkbook/en/1.0/implementing.the.domain.model.entries.and.authors)此问题是通过使用数据映射器解决的。
另外,您可以连接 2 个表,只需确保首先调用 select 对象的
setIntegrityCheck(false)
方法即可。文档说一行应该引用父表,并不意味着它不能:)If you're reading ZFSTDE, in chapter 9 (http://www.survivethedeepend.com/zendframeworkbook/en/1.0/implementing.the.domain.model.entries.and.authors) this problem is addressed by using a data mapper.
Also, you can join 2 tables, just be sure to first call on the select object the
setIntegrityCheck(false)
method. The docs say that a row should reference a parent table, doesn't mean it can not :)不要再将 Zend_Db_Table 视为您的“模型”。
您应该编写自己的、丰富的、以域为中心的模型类,将其置于控制器(和视图)和持久性逻辑(任何使用 Zend_Db/Zend_Db_Table/Zend_Db_Select 的逻辑)之间,以从数据库加载/存储数据。
Stop thinking about Zend_Db_Table as your "model".
You should write your own, rich, domain-centric model classes to sit between your controllers (and views), and your persistence logic (anything that uses Zend_Db/Zend_Db_Table/Zend_Db_Select) to load/store data from the database.
当然,您可以同时查询多个数据库表。在这里查看 ZF 官方文档 http://framework.zend.com/manual/en/zend.db.select.html#zend.db.select.building.join
至于获取单个用户的所有订单的示例,表关系就是答案 http://framework.zend。 com/manual/en/zend.db.table.relationships.html
Sure, you can query several db tables at the same time. Take a look at the official ZF docs here http://framework.zend.com/manual/en/zend.db.select.html#zend.db.select.building.join
As for your example with getting all orders of a single user, table relationships are the answer http://framework.zend.com/manual/en/zend.db.table.relationships.html