1

我尝试使用 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']);
        }
    }
}
4

1 回答 1

2

您的UserexchangeArray方法用于使用 db 值填充属性,它当前不引用id.

public function exchangeArray($data)
{
    // populate the id 
    $this->id = isset($data['id']) ? $data['id'] : null;

    $this->name = (isset($data['name'])) ?
    $data['name'] : null;
    $this->email = (isset($data['email'])) ?
    $data['email'] : null;
    if (isset($data['password']))
    {
        $this->setPassword($data['password']);
    }
}
于 2014-04-24T22:39:25.507 回答