Note that there are some explanatory texts on larger screens.

plurals
  1. POZend Framework 2 TableGateway fetchAll method returns empty result set but the table contains data
    text
    copied!<p>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.</p> <p>The issue described below platform-independent, I was able to reproduce it on both Windows and Linux, but I use only Windows now.</p> <p>I have this table in my MySQL server placed on my local machine:</p> <pre class="lang-sql prettyprint-override"><code>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); </code></pre> <p>I have this in my AdmMenuTable php class:</p> <pre class="lang-php prettyprint-override"><code>namespace VSMoney\Model; use Zend\Db\TableGateway\TableGateway; class AdmMenuTable { protected $tableGateway; public function __construct(TableGateway $tableGateway) { $this-&gt;tableGateway = $tableGateway; } public function fetchAll() { $resultSet = $this-&gt;tableGateway-&gt;select(); return $resultSet; } } </code></pre> <p>In another class I call the fetchAll() method to get the dataset and want to manipulate it in a foreach loop.</p> <p>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.</p> <p>There is no connection error when my code is running and the mysql log file dos not contains any problems.</p> <pre><code>object(Zend\Db\ResultSet\ResultSet)[272] protected 'allowedReturnTypes' =&gt; array (size=2) 0 =&gt; string 'arrayobject' (length=11) 1 =&gt; string 'array' (length=5) protected 'arrayObjectPrototype' =&gt; object(VSMoney\Model\AdmMenu)[238] public 'menu_id' =&gt; null public 'menu_name' =&gt; null public 'menu_desc' =&gt; null public 'menu_category' =&gt; null public 'is_active' =&gt; null protected 'returnType' =&gt; string 'arrayobject' (length=11) protected 'buffer' =&gt; null protected 'count' =&gt; int 7 protected 'dataSource' =&gt; object(Zend\Db\Adapter\Driver\Pdo\Result)[271] protected 'statementMode' =&gt; string 'forward' (length=7) protected 'resource' =&gt; object(PDOStatement)[244] public 'queryString' =&gt; string 'SELECT `admmenu`.* FROM `admmenu`' (length=33) protected 'options' =&gt; null protected 'currentComplete' =&gt; boolean false protected 'currentData' =&gt; null protected 'position' =&gt; int -1 protected 'generatedValue' =&gt; string '0' (length=1) protected 'rowCount' =&gt; int 7 protected 'fieldCount' =&gt; int 5 protected 'position' =&gt; int 0 </code></pre> <p>The serviceConfig looks like this:</p> <pre class="lang-php prettyprint-override"><code>'factories' =&gt; array( 'VSMoney\Model\AdmMenuTable' =&gt; function ($sm) { $tableGateway = $sm-&gt;get('AdmMenuTableGateway'); $table = new AdmMenuTable($tableGateway); return $table; }, 'AdmMenuTableGateway' =&gt; function ($sm) { //$dbAdapter = $sm-&gt;get('Zend\Db\Adapter\Adapter'); $config = $sm-&gt;get('config'); $config = $config['db']; $dbAdapter = new DbAdapter($config); $resultSetPrototype = new ResultSet(); $resultSetPrototype-&gt;setArrayObjectPrototype(new AdmMenu()); return new TableGateway('admmenu', $dbAdapter, null, $resultSetPrototype); }, </code></pre> <p>AdmMenu class:</p> <pre class="lang-php prettyprint-override"><code>&lt;?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-&gt;menu_id = (isset($data['menu_id'])) ? $data['menu_id'] : null; $this-&gt;menu_name = (isset($data['manu_name'])) ? $data['menu_name'] : null; $this-&gt;menu_desc = (isset($data['menu_desc'])) ? $data['menu_desc'] : null; $this-&gt;menu_category = (isset($data['menu_category'])) ? $data['menu_category'] : null; $this-&gt;is_active = (isset($data['is_active'])) ? $data['is_active'] : null; } } ?&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload