简体   繁体   English

在Zend Framework中处理联接的最佳方法?

[英]Best approach dealing with joins in Zend Framework?

Well it is more like a design question. 好吧,它更像是一个设计问题。 There are two ways I know to use joins in the Zend Framework 我知道在Zend框架中使用联接的两种方法

  1. Deal with it using instance of Zend table( its Select obj) 使用Zend表的实例处理它(其Select obj)
  2. Deal with it using the instance of Zend Db (its Select obj) 使用Zend Db实例(其Select obj)处理它

in the first approach it seems really strange to me - the some table have to be dealing with other tables too, which is not quite good, messy, at least when I read method name getData() - I assume that it deals with the data of its own. 在第一种方法中,我觉得真的很奇怪-某些表也必须与其他表打交道,这不是很好,很混乱,至少在我读取方法名称getData()时-我假设它处理数据它自己的。

Second one it is quite better, you can have some class that acts as a service - NodeService, and it will deal with tables node and node_translation ,but it also arises question why would we need Zend Table in this case. 第二个比较好,您可以拥有一个充当服务的类-NodeService,它将处理表node和node_translation,但也引起了一个问题,为什么在这种情况下我们需要Zend Table。 I hope I could explain myself good enough thanks. 我希望我能解释自己足够的感谢。

Technically, you are refering to Zend_Db_Select and Zend_Db_Table_Select . 从技术上讲,您指的是Zend_Db_SelectZend_Db_Table_Select Zend_Db and Zend_Db_Table do not allow for joining. Zend_DbZend_Db_Table不允许连接。

From the ZF manual on the Select API in chapter Zend_Db_Table : Zend_Db_Table一章中有关Select APIZF手册中

The Zend_Db_Table_Select object is an extension of the Zend_Db_Select object that applies specific restrictions to a query. Zend_Db_Table_Select对象是Zend_Db_Select对象的扩展,该扩展将特定限制应用于查询。 The enhancements and restrictions are: 增强和限制包括:

  • You can elect to return a subset of columns within a fetchRow or fetchAll query. 您可以选择返回fetchRow或fetchAll查询中的列的子集。 This can provide optimization benefits where returning a large set of results for all columns is not desirable. 在不希望为所有列返回大量结果的情况下,这可以提供优化的好处。
  • You can specify columns that evaluate expressions from within the selected table. 您可以指定用于选择表中表达式的列。 However this will mean that the returned row or rowset will be readOnly and cannot be used for save() operations. 但是,这将意味着返回的行或行集将被只读,并且不能用于save()操作。 A Zend_Db_Table_Row with readOnly status will throw an exception if a save() operation is attempted. 如果尝试执行save()操作,则具有readOnly状态的Zend_Db_Table_Row将引发异常。
  • You can allow JOIN clauses on a select to allow multi-table lookups. 您可以在select上允许JOIN子句以允许多表查找。
  • You can not specify columns from a JOINed tabled to be returned in a row/rowset. 您无法指定要在行/行集中返回的JOINed表中的列。 Doing so will trigger a PHP error. 这样做将触发PHP错误。 This was done to ensure the integrity of the Zend_Db_Table is retained. 这样做是为了确保保留Zend_Db_Table的完整性。 ie A Zend_Db_Table_Row should only reference columns derived from its parent table. 即Zend_Db_Table_Row应该仅引用从其父表派生的列。

The main difference is, Zend_Db_Table and Zend_Db_Table_Select act as a Table Data Gateway , which allows direct access to a table and it's rows and allows to call methods to crud the table, whereas Zend_Db_Select does not and is independent. 主要区别在于Zend_Db_TableZend_Db_Table_Select充当表数据网关 ,它可以直接访问表及其行,并可以调用方法来填充表,而Zend_Db_Select则不是,并且是独立的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM