2013-08-23 48 views
1

我想問一些關於Zend Framework 2中TableGateway的幫助。基本上,我按照教程一步一步地做了小的修改,不知道我錯過了什麼。Zend Framework 2 TableGateway fetchAll方法返回空結果集,但表中包含數據

下面描述的問題與平臺無關,我能夠在Windows和Linux上重現它,但我現在只使用Windows。

我這個表我的MySQL服務器放在我的本地機器上:

CREATE TABLE `admmenu` (
    `menu_id` int(11) NOT NULL, 
    `menu_name` varchar(255) NOT NULL, 
    `menu_desc` varchar(255) DEFAULT NULL, 
    `menu_category_id` int(11) DEFAULT NULL, 
    `is_active` int(11) NOT NULL DEFAULT '1', 
    PRIMARY KEY (`menu_id`) 
) ENGINE=InnoDB DEFAULT CHARSET=utf8; 

INSERT INTO `admmenu` 
VALUES (1,'add_outing',NULL,1,1), 
(2,'list_outings',NULL,1,1), 
(3,'add_income',NULL,2,1), 
(4,'list_incomes',NULL,2,1), 
(5,'add_organization',NULL,3,1), 
(6,'add_category',NULL,3,1), 
(7,'add_customer',NULL,3,1); 

我有這個在我的AdmMenuTable PHP類:

namespace VSMoney\Model; 

use Zend\Db\TableGateway\TableGateway; 

class AdmMenuTable { 

    protected $tableGateway; 

    public function __construct(TableGateway $tableGateway) { 
     $this->tableGateway = $tableGateway; 
    } 

    public function fetchAll() { 
     $resultSet = $this->tableGateway->select(); 
     return $resultSet; 
    } 
} 

在另一大類我所說的使用fetchall()方法來獲取數據集並想在foreach循環中對其進行操作。

我試圖轉儲我得到的數據集,但它是空的。如果我運行可以在mysql控制檯或mysql工作臺中的resultset對象中找到的sql查詢,那麼我得到了我想要的結果。

當我的代碼運行並且mysql日誌文件dos不包含任何問題時,沒有連接錯誤。

object(Zend\Db\ResultSet\ResultSet)[272] 
    protected 'allowedReturnTypes' => 
    array (size=2) 
     0 => string 'arrayobject' (length=11) 
     1 => string 'array' (length=5) 
    protected 'arrayObjectPrototype' => 
    object(VSMoney\Model\AdmMenu)[238] 
     public 'menu_id' => null 
     public 'menu_name' => null 
     public 'menu_desc' => null 
     public 'menu_category' => null 
     public 'is_active' => null 
    protected 'returnType' => string 'arrayobject' (length=11) 
    protected 'buffer' => null 
    protected 'count' => int 7 
    protected 'dataSource' => 
    object(Zend\Db\Adapter\Driver\Pdo\Result)[271] 
     protected 'statementMode' => string 'forward' (length=7) 
     protected 'resource' => 
     object(PDOStatement)[244] 
      public 'queryString' => string 'SELECT `admmenu`.* FROM `admmenu`' (length=33) 
     protected 'options' => null 
     protected 'currentComplete' => boolean false 
     protected 'currentData' => null 
     protected 'position' => int -1 
     protected 'generatedValue' => string '0' (length=1) 
     protected 'rowCount' => int 7 
    protected 'fieldCount' => int 5 
    protected 'position' => int 0 

的serviceConfig看起來是這樣的:

'factories' => array(

       'VSMoney\Model\AdmMenuTable' => function ($sm) { 
        $tableGateway = $sm->get('AdmMenuTableGateway'); 
        $table = new AdmMenuTable($tableGateway); 
        return $table; 
       }, 
       'AdmMenuTableGateway' => function ($sm) { 
        //$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter'); 
        $config = $sm->get('config'); 
        $config = $config['db']; 
        $dbAdapter = new DbAdapter($config); 
        $resultSetPrototype = new ResultSet(); 
        $resultSetPrototype->setArrayObjectPrototype(new AdmMenu()); 
        return new TableGateway('admmenu', $dbAdapter, null, $resultSetPrototype); 
       }, 

AdmMenu類:

<?php 

namespace VSMoney\Model; 

class AdmMenu{ 

    public $menu_id; 
    public $menu_name; 
    public $menu_desc; 
    public $menu_category; 
    public $is_active; 

    public function exchangeArray($data) { 
     $this->menu_id   = (isset($data['menu_id'])) ? $data['menu_id'] : null; 
     $this->menu_name  = (isset($data['manu_name'])) ? $data['menu_name'] : null; 
     $this->menu_desc  = (isset($data['menu_desc'])) ? $data['menu_desc'] : null; 
     $this->menu_category = (isset($data['menu_category'])) ? $data['menu_category'] : null; 
     $this->is_active  = (isset($data['is_active'])) ? $data['is_active'] : null; 
    } 
} 

?> 
+0

AdmMenu是什麼樣的?看起來結果集正在捕獲7條記錄,你怎樣調用fetchAll,你的問題似乎並沒有與事物的查詢結束。 – crowebird

+0

Crowebird:這個班是附件,感謝您的幫助! – SayusiAndo

+0

一切都很好,包括AdmMenu。你如何檢索ResultSet?你應該有這樣的$ rs = $ this-> getServiceLocator() - > get('VSMoney \ Model \ AdmMenuTable') - > fetchAll(); – crowebird

回答

4

基礎上的評論,這聽起來像你只是沒有對結果集循環。

$rs = $this->getServiceLocator()->get('VSMoney\Model\AdmMenuTable')->fetchAll(); 
foreach($rs as $r) { 
    //..do something on the result $r 
} 

你應該得到7環,因爲您的查詢似乎回到7項,併爲每個環路$ R將是AdmMenu的與各行的結果的情況下(如exchangeArray的結果定義) 。

+0

我將你的答案標記爲解決方案。我遇到了很多問題:打字錯誤,我沒有抓取適當的對象,並且弄亂了一些東西。謝謝你的幫助! – SayusiAndo

相關問題