1

我知道我可以通过 _referenceMap 定义关系,我知道我 con join 选择槽

$db->select()

但我需要的是在模型扩展中获取行集,Zend_Db_Table_Abstract然后按另一个表中引用列的值对其进行排序。

有一些解决方法可以做到这一点吗?

编辑:

这里是例子:

第一张表:

错误id , bugname , authorid

第二张表:

作者id , authorname

我有一个模型Model_Bugs extends Zend_Db_Table_Abstract

我想做这样的事情:

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

这意味着,我需要连接表并按列排序,该列不在模型表中。

感谢帮助

4

1 回答 1

0

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.

于 2011-07-12T23:46:56.353 回答