Skip to content

Commit

Permalink
Added Model
Browse files Browse the repository at this point in the history
  • Loading branch information
wingman007 committed Feb 8, 2013
1 parent 2397209 commit 3536d2e
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
language: php
php:
- "5.5"
- "5.4"
- "5.3"
20 changes: 19 additions & 1 deletion config/autoload/global.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,25 @@
* control, so do not include passwords or other sensitive information in this
* file.
*/

/*
return array(
// ...
);
*/

<?php
return array(
'db' => array(
'driver' => 'Pdo',
// 'dsn' => 'mysql:dbname=wingman;host=wingman-db.my.phpcloud.com',
'driver_options' => array(
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
),
),
'service_manager' => array(
'factories' => array(
'Zend\Db\Adapter\Adapter'
=> 'Zend\Db\Adapter\AdapterServiceFactory',
),
),
);
26 changes: 26 additions & 0 deletions module/Album/Module.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
<?php
namespace Album;

// Add these import statements:
use Album\Model\Album;
use Album\Model\AlbumTable;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;

class Module
{
public function getAutoloaderConfig()
Expand All @@ -21,4 +27,24 @@ public function getConfig()
{
return include __DIR__ . '/config/module.config.php';
}

// Add this method:
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);
},
),
);
}
}
16 changes: 16 additions & 0 deletions module/Album/src/Album/Model/Album.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
namespace Album\Model;

class Album
{
public $id;
public $artist;
public $title;

public function exchangeArray($data)
{
$this->id = (isset($data['id'])) ? $data['id'] : null;
$this->artist = (isset($data['artist'])) ? $data['artist'] : null;
$this->title = (isset($data['title'])) ? $data['title'] : null;
}
}
55 changes: 55 additions & 0 deletions module/Album/src/Album/Model/AlbumTable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
namespace Album\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('Form id does not exist');
}
}
}

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

0 comments on commit 3536d2e

Please sign in to comment.