Zend Framework 2 TableGateway fetchAll method returns empty result set but the table contains data

6.2k views Asked by At

I would like to ask some help regarding TableGateway in Zend Framework 2. Basically, I followed the tutorial step-by-step making small modifications but I don't know where I missed something.

The issue described below platform-independent, I was able to reproduce it on both Windows and Linux, but I use only Windows now.

I have this table in my MySQL server placed on my local machine:

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

I have this in my AdmMenuTable php class:

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

In another class I call the fetchAll() method to get the dataset and want to manipulate it in a foreach loop.

I tried to dump the dataset I get but it is empty. If I run the sql query which can be found in the resultset object in either mysql console or mysql workbench, then I got the result I want.

There is no connection error when my code is running and the mysql log file dos not contains any problems.

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

The serviceConfig looks like this:

'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 class:

<?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;
    }
}

?>
1

There are 1 answers

1
crowebird On BEST ANSWER

Based on the comments, it sounds like you are just not looping on the result set.

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

You should get 7 loops since your query appears to be returning 7 items, and for each loop $r will be an instance of AdmMenu with the results of that individual row (as defined by exchangeArray for the result).