我想就 Zend Framework 2 中的 TableGateway 寻求一些帮助。基本上,我按照教程一步一步地进行了一些小修改,但我不知道我错过了什么。
下面描述的问题与平台无关,我能够在 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 工作台的结果集对象中找到的 sql 查询,那么我得到了我想要的结果。
我的代码运行时没有连接错误,并且 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
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;
}
}
?>