开发者

When to use which Zend_Db class? Zend_Db_Select vs. Zend_Db_Table_Abstract

开发者 https://www.devze.com 2023-01-22 18:26 出处:网络
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.开发者_如何学JAVA

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.开发者_如何学JAVA

Is one more optimized for something than the other? Are joins "easier" with one or the other?

Thanks!


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消