1

我想从“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); ?>
4

2 回答 2

2

试试这个代码块并根据你改变 $condition

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();
    }

}
于 2013-10-28T07:05:13.490 回答
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);
于 2013-10-30T10:10:17.663 回答