开发者

How can I get Zend Db to return a rowset rather an array when performing a UNION query?

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开发者_如何学C 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?


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.


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");


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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜