按条件排序ResultSet Zend Framework 2?

时间:2013-12-03 13:39:34

标签: php mysql zend-framework2

当我调用fetchAll()函数时,我有一个tabel'cms',它按照id desc的顺序获取所有数据获取数据意味着我想通过fetchAll()函数传递orderby条件。

我的Cms.php页面是:

    <?php
    namespace Front\Model;
    use Zend\Db\TableGateway\AbstractTableGateway;

    class Cms extends AbstractTableGateway {

        public function __construct($adapter) {
            $this->table = 'cms';
            $this->adapter = $adapter;
        } 
        public function fetchAll($id) {
               return $this->select();
        }
        public function getCmsContent($id){
            $id  = (int) $id;
            $rowset = $this->select(array('id'=>$id));
            if (!$row = $rowset->current()){
                throw new \Exception ('Row not found');
            }
            return $row;
        }
    }

我的控制器是:FrontController.php是:        

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Front\Model\Cms;
use Front\Model\Setting;
use Front\Model\Slider;
class FrontController extends AbstractActionController
{
    public function indexAction()
    {   

        return        array('cms_data'=>$this->getCms()->fetchAll('1'));             
    }

    public function getCms(){
        return $this->getServiceLocator()->get('Front\Model\Cms');
    }

}

我的模特是:         

namespace Front;

class Module
{
    public function getAutoloaderConfig()
    {
        return array('Zend\Loader\StandardAutoloader' =>
            array('namespaces' =>
                array(__NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,),
            ),
        );
    }

    public function getConfig()
    {
        return include __DIR__ . '/config/module.config.php';
    }
    // Add this method:
    public function getServiceConfig()
    {
        return array(
            'factories' => array(

                'Front\Model\Cms' =>  function($sm) {
                    $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                    $table     = new \Front\Model\Cms($dbAdapter);
                    return $table;
                },

            ),
        );
    }
}
?>

我的观点:index.php页面是:

 <?php print_R($cms_data); ?>

1 个答案:

答案 0 :(得分:1)

摘自the manual

$select = new Select;
$select->order('id DESC'); // produces 'id' DESC

$select = new Select;
$select->order('id DESC')
     ->order('name ASC, age DESC'); // produces 'id' DESC, 'name' ASC, 'age' DESC

$select = new Select;
$select->order(array('name ASC', 'age DESC')); // produces 'name' ASC, 'age' DESC

相同的方法order适用于表网关。

相关问题