在执行 UNION 查询时,如何让 Zend Db 返回行集而不是数组?

发布于 2024-11-09 06:04:54 字数 510 浏览 2 评论 0原文

我有一个结果集,它是 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 技术交流群。

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

发布评论

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

评论(3

探春 2024-11-16 06:04:55

顺便说一句,如果您想指定 rowClass 或 rowsetClass,您可以通过调用 Zend_Db_Table 类的以下方法来指定 rowClass 或 rowsetClass:

使用 Zend_Db_Table 的方法 setRowsetClasssetRowClass

class RowTable extends Zend_Db_Table_Abstract {
    private static $_instance;
    protected $_name = "tableName";
    protected $_primary = "primary_key_id";

    # over-write the default class Zend_Db_Table_Rowset_Abstract
    # make sure the following classes are inside the include-path
    private function __construct($rowClass, $rowsetClass) {
        $this->setRowsetClass("ContributionList");
        $this->setRowClass("Contribution");
    }
}

$tableObject = new RowTable("RowClass", "RowsetClass");

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 and setRowClass:

class RowTable extends Zend_Db_Table_Abstract {
    private static $_instance;
    protected $_name = "tableName";
    protected $_primary = "primary_key_id";

    # over-write the default class Zend_Db_Table_Rowset_Abstract
    # make sure the following classes are inside the include-path
    private function __construct($rowClass, $rowsetClass) {
        $this->setRowsetClass("ContributionList");
        $this->setRowClass("Contribution");
    }
}

$tableObject = new RowTable("RowClass", "RowsetClass");
鸵鸟症 2024-11-16 06:04:55

顺便说一句,我知道这确实很旧,但是访问此页面的人应该知道,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 方法,该方法由 fetchAllfetchRow 方法使用。

干杯。

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 the Zend_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 the Zend_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 the getAdapter to get the select object, you are not getting the right select object, you are getting a Zend_Db_Select and you will need a Zend_Db_Table_Select so u can use on the Zend_Db_Table::_fetch method which is used by the fetchAll and fetchRow methods.

Cheers.

如果没有你 2024-11-16 06:04:54

你不能。您获取的结果集是一个组合,而不是数据库中的一组行,因此即使可能,也是一个非常糟糕的主意。

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.

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