何时使用哪个 Zend_Db 类? Zend_Db_Select 与 Zend_Db_Table_Abstract

发布于 2024-09-29 16:40:14 字数 134 浏览 9 评论 0原文

我认为可以使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

又怨 2024-10-06 16:40:14

由于历史原因,生成查询有几种不同的选项。

在 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文