使用Zend TableGateway的select()时无法检索id值

时间:2014-04-24 17:02:18

标签: php mysql zend-framework zend-framework2 tablegateway

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

1 个答案:

答案 0 :(得分: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']);
    }
}