我尝试使用 TableGateway 的select()
方法显示 MySql 表中的所有内容:
<section class="user_manager_index">
<h2>Users</h2>
<?php
$tableGateway = $this->tableGateway;
$rowset = $tableGateway->select();
foreach ($rowset as $row)
{
echo $row->id . ' ' . $row->name . ' ' . $row->email . '<br>';
}
?>
</section>
它从表中返回除了 id 值之外的所有值,它只是不写任何像 $row->id 的值是 NULL 的东西。我究竟做错了什么?
附加信息:我的 TableGateway 工厂:
'UserTableGateway' => function($sm)
{
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new User());
return new TableGateway('user', $dbAdapter, null, $resultSetPrototype);
}
附加信息:我的用户类:
class User
{
public $id;
public $name;
public $email;
public $password;
public function setPassword($clear_password)
{
$this->password = md5($clear_password);
}
public function exchangeArray($data)
{
$this->name = (isset($data['name'])) ?
$data['name'] : null;
$this->email = (isset($data['email'])) ?
$data['email'] : null;
if (isset($data['password']))
{
$this->setPassword($data['password']);
}
}
}