[英]Zend Framework 2 TableGateway fetchAll method returns empty result set but the table contains data
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. 我想在Zend Framework 2中询问有关TableGateway的一些帮助。基本上,我按照教程一步一步做了一些小修改,但我不知道我错过了什么。
The issue described below platform-independent, I was able to reproduce it on both Windows and Linux, but I use only Windows now. 下面描述的问题与平台无关,我能够在Windows和Linux上重现它,但我现在只使用Windows。
I have this table in my MySQL server placed on my local machine: 我把这个表放在我的本地机器上的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);
I have this in my AdmMenuTable php class: 我在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;
}
}
In another class I call the fetchAll() method to get the dataset and want to manipulate it in a foreach loop. 在另一个类中,我调用fetchAll()方法来获取数据集,并希望在foreach循环中对其进行操作。
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. 如果我运行可以在mysql控制台或mysql工作台中的resultset对象中找到的sql查询,那么我得到了我想要的结果。
There is no connection error when my code is running and the mysql log file dos not contains any problems. 我的代码运行时没有连接错误,并且mysql日志文件不包含任何问题。
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: 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 class: 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;
}
}
?>
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). 你应该得到7个循环,因为你的查询似乎返回了7个项目,并且对于每个循环,$ r将是AdmMenu的一个实例,其中包含该单独行的结果(由结果的exchangeArray
定义)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.