简体   繁体   中英

How to do a Zend DbTable fetchAll in descending order

I'm writing a Zend 1.12 application involving election results. I can use the DbTable fetchAll to retrieve only candidates in a specific riding, but I'd like to order them by vote from most to least (DSC) (instead of the default least to most (ASC)).

//class Application_Model_CandidateMapper
public function fetchForRiding($riding)
{
    $where = 'riding = %s';
    $resultSet = $this->getDbTable()->fetchAll(sprintf($where, $riding), 'votes');
    //blah blah blah
    return $candidates
}

This gets the candidates for the riding and orders them by vote, but in ASC instead of DSC. I tried to jack 'DSC' into the fetchAll arguments a few different ways and the database complained it was being asked to ORDER BY 'votes DSC' ASC .

First: its DESC , not DSC :) Simply use zend functions instead of plain sql (as mentioned in the comment):

public function fetchForRiding($riding)
{
    $select = $this->getDbTable()->select();
    $select->where('riding = ?', $riding);
    $select->order('votes DESC');

    $resultSet = $this->getDbTable()->fetchAll($select);

    [...]

    return $candidates
}

(untested)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM