开发者

Order Zend_Db_Table rowset by reference column

i know i can define relationships through _referenceMap, i know that i con join selects trough

$db->select()

开发者_运维技巧

But what i need is to fetch rowset in model extending Zend_Db_Table_Abstract and then order it by value of referenced column from another table.

Is there some workaround to do that?

edit:

heres is the example:

first table:

table bugs columns id, bugname, authorid

second table:

table authors columns id, authorname

I have a model Model_Bugs extends Zend_Db_Table_Abstract

I want to make something like this:

$model->fetchAll($model->select()->order('authorname ASC'))

This means, that i need to join tables and sort by a column, which is not in the model table.

thanks for help

Jan


I would add a method in Model_Bugs like so:

public function fetchBugsByAuthorname() {

    $bugTable = $this;
    $bugTableName = $this->info('name');
    $authorsTable = new Model_Authors();
    $authorsTableName = $authorsTable->info('name');

    $select = $bugTable->select()
        ->setIntegrityCheck(false)
        ->from($bugTable, array('id', 'bugname', 'authorid'))
        ->join($authorsTableName, 
            "$bugTableName.authorid = $authorsTableName.id", 
            array("authorname"))
        ->order("$authorsTableName.authorname asc");
    $result = $bugTable->fetchAll($select);

    return $result;
}

But to do this you have to turn off ZF's table integrity checking (setIntegrityCheck(false) above), which means you won't be able to directly call save() on the resulting rows. But if it's for a read-only purpose, it will work.

If you needed to save rowsets back to the database, you may have to first select the author ID's from Model_Authors in the order you want them, and then re-order your Model_Bugs query accordingly. It's messier but it can work.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜