在ResultSet Zend Framework 2的查询中执行?

时间:2013-10-27 11:44:25

标签: php mysql zend-framework2

我想从'cms'表中获得3个单独的行。但是我一直只有第一行为什么?

我的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 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()
    {   

        $this->layout()->slider_data = $this->getSlider()->fetchAll();
        $this->layout()->setting_data = $this->getSetting()->getSettingContent(1);
        return array('cms_data'=>$this->getCms()->getCmsContent('1,2,3'));           
    }
    public function getSlider(){
        return $this->getServiceLocator()->get('Front/Model/Slider');
    }
    public function getCms(){
        return $this->getServiceLocator()->get('Front\Model\Cms');
    }
    public function getSetting(){
        return $this->getServiceLocator()->get('Front/Model/Setting');
    }
}

我的模特是:         

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\AlbumTable' =>  function($sm) {
                    $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                    $table     = new \Front\Model\AlbumTable($dbAdapter);
                    return $table;
                },
                'Front\Model\Cms' =>  function($sm) {
                    $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                    $table     = new \Front\Model\Cms($dbAdapter);
                    return $table;
                },
                'Front\Model\Setting' =>  function($sm) {
                    $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                    $table     = new \Front\Model\Setting($dbAdapter);
                    return $table;
                },
                'Front\Model\Slider' => function($sm) {
                    $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                    $table     = new \Front\Model\Slider($dbAdapter);
                    return $table;
                },
            ),
        );
    }
}
?>

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

 <?php print_R($cms_data); ?>

2 个答案:

答案 0 :(得分:2)

尝试此代码块并根据您

更改$条件
public function getCmsContent($id){
    $id  = (int) $id;
    $select = new Select();
    $select->from(array('t' => $this->table))
            ->where(array($condition));

    $statement = $this->adapter->createStatement();
    $select->prepareStatement($this->adapter, $statement);
    $result = $statement->execute();

    $rows = array();
    if ($result->count()) {
         $rows = new ResultSet();
         return $rows->initialize($result)->toArray();
    }

}

答案 1 :(得分:1)

你试试:

class Cms extends AbstractTableGateway {

    public function __construct($adapter) {
        $this->table = 'cms';
        $this->adapter = $adapter;
    }
    public function getCmsContent($id){
        return $this->select(array(
                new Expression('id IN (?)', array($id)),
        ));
    }
}

获取内容

$contents = $this->getCms()->getCmsContent(array(1, 2, 3));

$results = array();
foreach($contents as $content){
     $results[] = $content;
}
print_r($results);