Thứ Năm, 24 tháng 4, 2014

TableGateway [Creating a Communication Application]

The TableGateway pattern is used for creating an object tharepresents a table in the database; in this example, we will need a TableGateway object for the User table.
In this task, we will be creating a new user model, creating table in MySQL database to save the registration data using TableGateway tstorregistration datto the table. We will, finally, connect our registration form toUserTable so that neregistrations arstored in the database. Perform the following stepto do so:

1.       A netable needs to be created tstore the registration information in the MySQL database:
CREATE TABLE user (
id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT, name TEXT NOT NULL,
email VARCHAR(255) NOT NULL, password TEXT NOT NULL, PRIMARY KEY (id),
UNIQUE INDEX idx_email(email)
);

2.       The application's global configuration needs to be modified to add references to the database connection as shown in the following snippet. This is available under
<Application_Home>/config/autoload/global.php.
return array(
'db' => array(
'driver'         => 'Pdo',
'dsn'            => 'mysql:dbname=test;host=localhost', 'username'          => 'db_user',
'password'         => '', 'driver_options' => array(
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
),
),
'service_manager' => array( 'factories' => array(
'Zend\Db\Adapter\Adapter'
=> 'Zend\Db\Adapter\AdapterServiceFactory',
),
),
);


3.       Create a new model for the User class. This needs to be created under
src/Users/Model/User.php.
<?php
namespace Users\Model; class User
{
public $id; public $name; public $email; public $password;
}

4.       The User model will define the setPassword() and the exchangeArray()
methods:
1.       Implement a setPassword() method which will assign a MD5 version passworto the UserTable entity for storage:

public function setPassword($clear_password)
{
$this->password = md5($clear_password);
}

2.       Implement the exchangeArray() method; this method is used while mapping the User entity to the UserTable entity:

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"]);
}
}

5.       Create a netable reference for User. This needs to be created under src/Users/ Model/UserTable.php:
<?php
namespace Users\Model;
use Zend\Db\Adapter\Adapter;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway; class UserTable


{
protected $tableGateway;
public function   construct(TableGateway $tableGateway)
{
$this->tableGateway = $tableGateway;
}
public function saveUser(User $user)
{
$data = array(
'email' => $user->email, 'name'       => $user->name, 'password'  => $user->password,
);
$id = (int)$user->id; if ($id == 0) {
$this->tableGateway->insert($data);
} else {
if ($this->getUser($id)) {
$this->tableGateway->update($data, array('id' => $id));
} else {
throw new \Exception('User ID does not exist');
}
}
}
public function getUser($id)
{
$id  = (int) $id;
$rowset = $this->tableGateway->select(array('id' => $id));
$row = $rowset->current(); if (!$row) {
throw new \Exception("Could not find row $id");
}
return $row;
}
}

6.       Now we can use UserTable to save neregistrations to the database. To savregistrations, we need to make changes to the RegisterController class. First, we will create a new function for saving userregistration:
protected function createUser(array $data)
{
$sm = $this->getServiceLocator();
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$resultSetPrototype = new \Zend\Db\ResultSet\ResultSet();


$resultSetPrototype->setArrayObjectPrototype(new
\Users\Model\User);
$tableGateway = new \Zend\Db\TableGateway\TableGateway('user',
$dbAdapter, null, $resultSetPrototype);

$user = new User();
$user->exchangeArray($data);
$userTable = new UserTable($tableGateway);
$userTable->saveUser($user); return true;
}
7.       Next, we need to make sure that the processAction() method calls this function before redirecting to the confirmation page:
// Create user
$this->createUser($form->getData());
8.       Open the registration page in your favourite browser and use the MySQL database to check if the registration information is properly stored in the database. The registration confirmation page should look like thefollowing screenshot:


You can check the MySQL database to see if the records have been inserted properly:


What just happened?
We have now modified the form to save new user registrations to the database; our nexstep will be to set up authentication based on the information stored in the database.

Không có nhận xét nào:

Đăng nhận xét