在执行 UNION 查询时,如何让 Zend Db 返回行集而不是数组?
我有一个结果集,它是 MySQL UNION 查询的结果。我用来获取数据的代码是:
$union_select = $PagesTable->getAdapter()->select()
->union(array('(' . $legal_resources_select . ')', '(' . $pages_select . ')'));
$PagesTable->getAdapter()->fetchAll($union_select)
$PagesTable
extends Zend_Db_Table_Abstract
。完整的选择太大,无法发布在这里,我认为它与这个特定问题无关。如果我错了请告诉我。
目前,这返回一个结果数组,但我希望它返回一个行集对象。我还必须能够指定 $_rowClass
。这是必要的,因此我可以添加用于格式化和操作返回值的方法。
这可能吗?
I have a result set that is the result of a MySQL UNION query. The code I am using to fetch the data is:
$union_select = $PagesTable->getAdapter()->select()
->union(array('(' . $legal_resources_select . ')', '(' . $pages_select . ')'));
$PagesTable->getAdapter()->fetchAll($union_select)
$PagesTable
extends Zend_Db_Table_Abstract
. The full select is too big to post here and I don't think it is relevant to this particular question. If I am wrong let me know.
Currently this is returning an array of results, but I want it to return a rowset object. I must also be able to specify the $_rowClass
. This is necessary so I can add methods for formatting and manipulating the returned values.
Is this possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
顺便说一句,如果您想指定 rowClass 或 rowsetClass,您可以通过调用 Zend_Db_Table 类的以下方法来指定 rowClass 或 rowsetClass:
使用 Zend_Db_Table 的方法
setRowsetClass
和setRowClass
:BTW, if you want to specify the rowClass or rowsetClass, you can do that with the Zend_Db_Table class by calling the following methods
Using Zend_Db_Table's methods
setRowsetClass
andsetRowClass
:顺便说一句,我知道这确实很旧,但是访问此页面的人应该知道,
Zend_Db_Adapter
总是返回数组,因此当您使用Zend_Db_Table::getAdapter
code> 方法,您实际上摆脱了表类并使用返回数组的适配器类中包含的 fetch 方法,而不是在数据网关模式下返回对象的Zend_Db_Table::_fetch
。所以第一个答案是错误的,结果集将是一个包含许多行对象的行集,但没有所谓的数据水合作用,因此预计行对象上有许多冗余数据。
我看到我工作的很多人都犯了这个错误,我想知道为什么这么多人使用
getAdapter
方法。另一件要提到的是,当您使用 getAdapter 获取选择对象时,您没有获得正确的选择对象,而是获得了 Zend_Db_Select ,并且您将需要一个Zend_Db_Table_Select
,这样你就可以使用Zend_Db_Table::_fetch
方法,该方法由fetchAll
和fetchRow
方法使用。干杯。
By the way, I know this is really old, but the ones who get to this page should know that, a
Zend_Db_Adapter
always returns arrays, so when you use theZend_Db_Table::getAdapter
method, you are actually getting away from your table class and using the fetch method contained on the adapter class which returns array, rather than theZend_Db_Table::_fetch
which returns objects under the Data Gateway pattern.So the first answer is wrong, the result set will be a row-set with many row objects but without what doctrine calls data hydration, so expect many redundant data on the row objects.
I see this mistake done by a lot of people where I work, I wonder why so many people uses the
getAdapter
method. Another thing to mention is that, when you use thegetAdapter
to get the select object, you are not getting the right select object, you are getting aZend_Db_Select
and you will need aZend_Db_Table_Select
so u can use on theZend_Db_Table::_fetch
method which is used by thefetchAll
andfetchRow
methods.Cheers.
你不能。您获取的结果集是一个组合,而不是数据库中的一组行,因此即使可能,也是一个非常糟糕的主意。
Zend_Db_Table 是表数据网关模式的实现,而不是 Active Record。
您所描述的通常在 Active Record 下是可能的,为此,我建议查看 Doctrine 1.2 而不是 Zend_Db_Table。
You cannot. The result set you are fetching is a composite, and not a set of Rows from the DB, so even if it were possible, its a pretty bad idea.
Zend_Db_Table is an implementation of the Table Data Gateway pattern more than Active Record.
What you describe would be possible usually under Active Record, and to do so, I'd suggest looking at Doctrine 1.2 rather than Zend_Db_Table.