何时使用哪个 Zend_Db 类? Zend_Db_Select 与 Zend_Db_Table_Abstract
我认为可以使用 Zend_Db_Select 或 Zend_Db_Table_Abstract 编写选择查询,但我不明白何时使用两者中的哪一个。
其中一个在某些方面是否比另一个更优化?与其中之一的连接“更容易”吗?
谢谢!
I think it's possible to write select queries with either Zend_Db_Select or Zend_Db_Table_Abstract, but I don't understand when to use which of the two.
Is one more optimized for something than the other? Are joins "easier" with one or the other?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于历史原因,生成查询有几种不同的选项。
在 Zend Framework 的早期版本中,Zend_Db_Tables 有一个方法 fetchAll ,其参数如下: order、offset 和 limit 可用于从表中获取行。开发人员很快发现这种方法的局限性。如何添加 GROUP BY 子句?
Zend_Db_Select 是为了解决这个问题而发明的,您会注意到从 ZF 1.5 开始, fetchAll 和相关方法接受 Zend_Db_Select 实例作为第一个参数。现在不推荐使用 fetchAll 的其他参数,您应该传入 SQL 字符串或 Zend_Db_Select 对象。
Zend_Db_Select 只是一个用于构建 SQL 查询的编程接口。它非常适合根据用户输入或不同因素更改 SQL 的某些部分,因为您无需操作字符串,只需更改方法调用和参数即可。
如果调用 Zend_Db_Table 的 select 方法,Zend_Db_Table 将返回一个 Zend_Db_Table_Select (Zend_Db_Select 子类)实例,其表名是预定义的 - 这是 Zend_Db_Select 和 Zend_Db_Table_Select 之间的唯一区别。
您真正考虑的是是否使用 Zend_Db_Select 还是手动编写 SQL。 Zend_Db_Select 并不是无限灵活,但它很容易阅读、操作和使用。
There are a few different options in producing queries, for historical reasons.
In early versions of Zend Framework, Zend_Db_Tables had a method fetchAll with parameters where, order, offset and limit which you could use to fetch rows from a table. Developers soon found limitations with this approach. How would you add a GROUP BY clause?
Zend_Db_Select was invented to solve this problem, and you'll notice that since ZF 1.5, the fetchAll and related methods accept a Zend_Db_Select instance as the first parameter. Using the other parameters of fetchAll is now deprecated and you should pass in either an SQL string or a Zend_Db_Select object.
A Zend_Db_Select is simply a programmatic interface for building an SQL query. It's great for changing parts of the SQL based on user input or different factors, as instead of manipulating strings, you can just change the method calls and arguments.
Zend_Db_Table will return a Zend_Db_Table_Select (a Zend_Db_Select subclass) instance with its table name predefined if you call its select method - this is about the only difference between Zend_Db_Select and Zend_Db_Table_Select.
Your consideration really is whether to use Zend_Db_Select or to write SQL manually. Zend_Db_Select isn't infinitely flexible but it is easy to read, manipulate and work with.