使用ResultSet Zend Framework 2的Where条件执行查询?

时间:2013-11-27 13:41:09

标签: php mysql zend-framework zend-db-table

我有一个tabel'cms',其中包括带有3个单独父类别(类型)的cms页面。当我调用fetchAll()函数获取所有数据,但我只想获取父类别(类型)='1'的数据意味着我想在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 :(得分:0)

你的问题不明确。您是否在询问如何在查询中添加Where子句?如果是这样,那么就是这样:

use Zend\Db\Sql\Predicate\Operator;

public function fetchAll($id) {

   return $this->select()
        ->where(new Operator('type', Operator::OPERATOR_EQUAL_TO, $id);
}