How to use the models in Zend Framework 2.5

In this tutorial I will explain how to use the models in Zend Framework 2.5. I have also created a video tutorial where you can see how to use the Zend Framework's models.

First, I need to connect Zend Framework to database. I can do this in config\autoload\global.php by adding this code:

return array(
'db' => array(
'driver' => 'Pdo',
'dsn' => 'mysql:dbname=zend;hostname=localhost',
'username' => 'user2',
'password' => '123456',
'driver_options' => array(
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
),
),
'service_manager' => array(
'factories' => array(
'Zend\Db\Adapter\Adapter' => 'Zend\Db\Adapter\AdapterServiceFactory',
),
),
);

Next, I will create the folder Model in module\Newapp\src\Newapp.

In the folder Model I will create two files: Album.php and AlbumTable.php. The models in all PHP frameworks helps us to extract data from database, insert new data, delete and update data in database.

In the first file Album.php I will add this code:


id = (!empty($data['id'])) ? $data['id'] : null;
$this->artist = (!empty($data['artist'])) ? $data['artist'] : null;
$this->title = (!empty($data['title'])) ? $data['title'] : null;
}
}

In the file AlbumTable.php(this file will help me to manage the data in the database) I will add this code:


namespace Newapp\Model;

use Zend\Db\TableGateway\TableGateway;

class AlbumTable
{
protected $tableGateway;

public function __construct(TableGateway $tableGateway)
{
$this->tableGateway = $tableGateway;
}

public function fetchAll()
{
$resultSet = $this->tableGateway->select();
return $resultSet;
}

public function getAlbum($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;
}

public function saveAlbum(Album $album)
{
$data = array(
'artist' => $album->artist,
'title' => $album->title,
);

$id = (int) $album->id;
if ($id == 0) {
$this->tableGateway->insert($data);
} else {
if ($this->getAlbum($id)) {
$this->tableGateway->update($data, array('id' => $id));
} else {
throw new \Exception('Album id does not exist');
}
}
}

public function deleteAlbum($id)
{
$this->tableGateway->delete(array('id' => (int) $id));
}
}

Then, In my Controller I will add a protected variable in my controller:

protected $albumTable;

Next, I will use this function in my controller:


public function getAlbumTable()
{
if (!$this->albumTable) {
$sm = $this->getServiceLocator();
$this->albumTable = $sm->get('Newapp\Model\AlbumTable');
}
return $this->albumTable;
}

It will help me to use the class AlbumTable to extract, delete, update and insert data in the database.

Next, In the file module\Newapp\Module.php I will connect my model:

use Newapp\Model\AlbumTable;

Also I will use two namespaces to connect my model to database:

use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;

Also, I will add a new function:


public function getServiceConfig()
{
return array(
'factories' => array(
'Album\Model\AlbumTable' => function($sm) {
$tableGateway = $sm->get('AlbumTableGateway');
$table = new AlbumTable($tableGateway);
return $table;
},
'AlbumTableGateway' => function ($sm) {
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new Album());
return new TableGateway('album', $dbAdapter, null, $resultSetPrototype);
},
),
);
}

Video tutorial:









Leave a Comment

How to use pagination in Zend Framework 2.5 - Scrisoft Tutorials
How to use pagination in Zend Framework 2.5 - Scrisoft Tutorials commented on June 30, 2015
[…] a handy pagination controller in our Zend Framework projects. In my last tutorial I have explained how to use the models in Zend Framework 2.5 and in this tutorial I will explain how to add pagination in a Zend Framework […]