duandie5707 2014-04-11 17:07
浏览 18
已采纳

如何按降序执行Zend DbTable fetchAll

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.

  • 写回答

1条回答 默认 最新

  • douvcpx6526 2014-04-12 10:24
    关注

    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)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 MATLAB联合adams仿真卡死如何解决(代码模型无问题)
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改