Question

I try to show all the content I have in MySql table by using TableGateway's select() method:

<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>

And it returns all the values from the table fine except for the id value, it just doesn't write anything like the value of $row->id is NULL. What am I doing wrong?

Additional information: My TableGateway factory:

'UserTableGateway' => function($sm)
   {
        $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
        $resultSetPrototype = new ResultSet();
        $resultSetPrototype->setArrayObjectPrototype(new User());
        return new TableGateway('user', $dbAdapter, null, $resultSetPrototype);
   }

Additional information: My User class:

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']);
        }
    }
}
Was it helpful?

Solution

Your User class exchangeArray method is used to populate properties with db values, it currently doesn't reference the 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']);
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top