Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
tmp
<?php
copy(__DIR__ . '/music.db.original', __DIR__ . '/music.db');
include 'vendor/autoload.php';
return new Zend\Db\Adapter\Adapter(array(
// Sqlite Configuration
'driver' => 'Pdo',
'dsn' => 'sqlite:' . __DIR__ . '/music.db',
'driver_options' => array(
'buffer_results' => true
)
));
{
"name": "ralphschindler/modeling-code",
"authors": [
{
"name": "Ralph Schindler",
"email": "ralph@ralphschindler.com"
}
],
"require": {
"zendframework/zendframework": "2.2.*"
}
}
{
"_readme": [
"This file locks the dependencies of your project to a known state",
"Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file"
],
"hash": "2d6102df79f19248e1803d15d03c32b3",
"packages": [
{
"name": "zendframework/zendframework",
"version": "2.2.4",
"source": {
"type": "git",
"url": "https://github.com/zendframework/zf2.git",
"reference": "5b03d868755c181fc9b5dc56c1f8c26e73ca0bed"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/zendframework/zf2/zipball/5b03d868755c181fc9b5dc56c1f8c26e73ca0bed",
"reference": "5b03d868755c181fc9b5dc56c1f8c26e73ca0bed",
"shasum": ""
},
"require": {
"php": ">=5.3.3"
},
"replace": {
"zendframework/zend-authentication": "self.version",
"zendframework/zend-barcode": "self.version",
"zendframework/zend-cache": "self.version",
"zendframework/zend-captcha": "self.version",
"zendframework/zend-code": "self.version",
"zendframework/zend-config": "self.version",
"zendframework/zend-console": "self.version",
"zendframework/zend-crypt": "self.version",
"zendframework/zend-db": "self.version",
"zendframework/zend-debug": "self.version",
"zendframework/zend-di": "self.version",
"zendframework/zend-dom": "self.version",
"zendframework/zend-escaper": "self.version",
"zendframework/zend-eventmanager": "self.version",
"zendframework/zend-feed": "self.version",
"zendframework/zend-file": "self.version",
"zendframework/zend-filter": "self.version",
"zendframework/zend-form": "self.version",
"zendframework/zend-http": "self.version",
"zendframework/zend-i18n": "self.version",
"zendframework/zend-inputfilter": "self.version",
"zendframework/zend-json": "self.version",
"zendframework/zend-ldap": "self.version",
"zendframework/zend-loader": "self.version",
"zendframework/zend-log": "self.version",
"zendframework/zend-mail": "self.version",
"zendframework/zend-math": "self.version",
"zendframework/zend-memory": "self.version",
"zendframework/zend-mime": "self.version",
"zendframework/zend-modulemanager": "self.version",
"zendframework/zend-mvc": "self.version",
"zendframework/zend-navigation": "self.version",
"zendframework/zend-paginator": "self.version",
"zendframework/zend-permissions-acl": "self.version",
"zendframework/zend-permissions-rbac": "self.version",
"zendframework/zend-progressbar": "self.version",
"zendframework/zend-resources": "self.version",
"zendframework/zend-serializer": "self.version",
"zendframework/zend-server": "self.version",
"zendframework/zend-servicemanager": "self.version",
"zendframework/zend-session": "self.version",
"zendframework/zend-soap": "self.version",
"zendframework/zend-stdlib": "self.version",
"zendframework/zend-tag": "self.version",
"zendframework/zend-test": "self.version",
"zendframework/zend-text": "self.version",
"zendframework/zend-uri": "self.version",
"zendframework/zend-validator": "self.version",
"zendframework/zend-version": "self.version",
"zendframework/zend-view": "self.version",
"zendframework/zend-xmlrpc": "self.version"
},
"require-dev": {
"doctrine/annotations": ">=1.0",
"ircmaxell/random-lib": "dev-master",
"ircmaxell/security-lib": "dev-master",
"ocramius/proxy-manager": "0.4.*",
"phpunit/phpunit": "3.7.*"
},
"suggest": {
"doctrine/annotations": "Doctrine Annotations >=1.0 for annotation features",
"ext-intl": "ext/intl for i18n features (included in default builds of PHP)",
"ircmaxell/random-lib": "Fallback random byte generator for Zend\\Math\\Rand if OpenSSL/Mcrypt extensions are unavailable",
"ocramius/proxy-manager": "ProxyManager to handle lazy initialization of services",
"zendframework/zendpdf": "ZendPdf for creating PDF representations of barcodes",
"zendframework/zendservice-recaptcha": "ZendService\\ReCaptcha for rendering ReCaptchas in Zend\\Captcha and/or Zend\\Form"
},
"bin": [
"bin/classmap_generator.php",
"bin/pluginmap_generator.php",
"bin/templatemap_generator.php"
],
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "2.2-dev",
"dev-develop": "2.3-dev"
}
},
"autoload": {
"psr-0": {
"Zend\\": "library/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"BSD-3-Clause"
],
"description": "Zend Framework 2",
"homepage": "http://framework.zend.com/",
"keywords": [
"framework",
"zf2"
],
"time": "2013-08-26 15:09:04"
}
],
"packages-dev": [
],
"aliases": [
],
"minimum-stability": "stable",
"stability-flags": [
],
"platform": [
],
"platform-dev": [
]
}
<?php
// bootstrap application
namespace {
$db = include 'bootstrap.php';
}
// controller
namespace Application\Controller {
use Application\Model;
class ArtistController {
public function __construct(Model\ArtistTable $artistTable, Model\AlbumTable $albumTable) {
$this->artistTable = $artistTable;
$this->albumTable = $albumTable;
}
public function showArtistsAction() {
$artists = $this->artistTable->select();
foreach ($artists as $artist) {
echo $artist['name'] . PHP_EOL;
$albums = $this->albumTable->select(['artist_id' => $artist['id']]);
foreach ($albums as $i => $album) {
echo ' ' . ($i+1) . ': ' . $album['title'] . ' (' . $album['release_date'] . ')' . PHP_EOL;
}
}
}
}
}
// Model Code
namespace Application\Model {
use Zend\Db\TableGateway\TableGateway;
use Zend\Db\Adapter\AdapterInterface;
class ArtistTable extends TableGateway {
public function __construct(AdapterInterface $adapter)
{
parent::__construct('artist', $adapter);
}
}
class AlbumTable extends TableGateway {
public function __construct(AdapterInterface $adapter)
{
parent::__construct('album', $adapter);
}
}
}
// execution
namespace {
$controller = new Application\Controller\ArtistController(
new Application\Model\ArtistTable($db),
new Application\Model\AlbumTable($db)
);
$controller->showArtistsAction();
}
<?php
// bootstrap application
namespace {
$db = include 'bootstrap.php';
}
// controller
namespace Application\Controller {
use Application\Model;
class ArtistController {
public function showArtistsAction() {
/** @var Model\ArtistActiveRecord[] $artists */
$artists = Model\ArtistActiveRecord::findAll();
foreach ($artists as $artist) {
echo $artist->getName() . PHP_EOL;
/** @var Model\AlbumActiveRecord[] $albums */
$albums = Model\AlbumActiveRecord::findAllForArtistId($artist->getId());
foreach ($albums as $i => $album) {
echo ' ' . ($i+1) . ': ' . $album->getTitle() . ' (' . $album->getReleaseDate() . ')' . PHP_EOL;
}
}
}
}
}
// Model Code
namespace Application\Model {
use Zend\Db\TableGateway\TableGateway;
use Zend\Db\Adapter\AdapterInterface;
abstract class ActiveRecord {
protected static $table; // Late Static Binding
public static function setTableGateway(TableGateway $table) {
static::$table = $table;
}
}
class ArtistActiveRecord extends ActiveRecord {
protected static $table; // Late Static Binding
protected $row = array();
public static function findAll() {
$rows = array();
foreach (static::$table->select() as $row) {
$rows[] = new static($row);
}
return $rows;
}
public function __construct($row) {
$this->row = $row;
}
public function getId() {
return $this->row['id'];
}
public function getName() {
return $this->row['name'];
}
// public function save()
// public function delete()
}
class AlbumActiveRecord extends ActiveRecord {
protected static $table; // Late Static Binding
protected $row = array();
public static function findAllForArtistId($artistId) {
$rows = array();
foreach (static::$table->select(['artist_id' => $artistId]) as $row) {
$rows[] = new static($row);
}
return $rows;
}
public function __construct($row) {
$this->row = $row;
}
public function getTitle() {
return $this->row['title'];
}
public function getReleaseDate() {
return $this->row['release_date'];
}
// public function save()
// public function delete()
}
}
// execution
namespace {
use Application\Model\ArtistActiveRecord;
use Application\Model\AlbumActiveRecord;
ArtistActiveRecord::setTableGateway(new Zend\Db\TableGateway\TableGateway('artist', $db));
AlbumActiveRecord::setTableGateway(new Zend\Db\TableGateway\TableGateway('album', $db));
$controller = new Application\Controller\ArtistController();
$controller->showArtistsAction();
}
<?php
// bootstrap application
namespace {
$db = include 'bootstrap.php';
}
// controller
namespace Application\Controller {
use Application\Model;
class ArtistController {
public function __construct(Model\ArtistDataMapper $artistDataMapper) {
$this->artistDataMapper = $artistDataMapper;
}
public function showArtistsAction() {
$artists = $this->artistDataMapper->findAll();
foreach ($artists as $artist) {
echo $artist['name'] . PHP_EOL;
foreach ($artist['albums'] as $i => $album) {
echo ' ' . ($i+1) . ': ' . $album['title'] . ' (' . $album['release_date'] . ')' . PHP_EOL;
}
}
}
}
}
// Model Code
namespace Application\Model {
use Zend\Db\TableGateway\TableGateway;
use Zend\Db\Adapter\AdapterInterface;
class ArtistDataMapper {
public function __construct(AdapterInterface $adapter) {
$this->artistTable = new TableGateway('artist', $adapter);
$this->albumTable = new TableGateway('album', $adapter);
}
public function findAll() {
$artists = $this->artistTable->select()->toArray();
foreach ($artists as $id => $artist) {
$artists[$id]['albums'] = $this->albumTable->select(['artist_id' => $artist['id']])->toArray();
}
return $artists;
}
// public function save(array )
// public function delete()
}
}
// execution
namespace {
$controller = new Application\Controller\ArtistController(
new Application\Model\ArtistDataMapper($db)
);
$controller->showArtistsAction();
}
<?php
// bootstrap application
namespace {
$db = include 'bootstrap.php';
}
// controller
namespace Application\Controller {
use Application\Model;
class ArtistController {
public function __construct(Model\ArtistRepository $artistRepository) {
$this->artistDataMapper = $artistRepository;
}
public function showArtistsAction() {
$artists = $this->artistDataMapper->findAll();
foreach ($artists as $artist) {
echo $artist->getName() . PHP_EOL;
foreach ($artist->getAlbums() as $i => $album) {
echo ' ' . ($i+1) . ': ' . $album->getTitle() . ' (' . $album->getReleaseDate() . ')' . PHP_EOL;
}
}
}
}
}
// Model Code
namespace Application\Model {
use Zend\Db\TableGateway\TableGateway;
use Zend\Db\Adapter\AdapterInterface;
use Zend\Db\ResultSet\HydratingResultSet;
use Zend\Stdlib\Hydrator\ClassMethods as ClassMethodsHydrator;
class ArtistRepository {
public function __construct(AdapterInterface $adapter) {
$this->artistTable = new TableGateway('artist', $adapter, null,
new HydratingResultSet(new ClassMethodsHydrator, new ArtistEntity)
);
$this->albumTable = new TableGateway('album', $adapter, null,
new HydratingResultSet(new ClassMethodsHydrator, new AlbumEntity)
);
}
public function findAll() {
$artists = $this->artistTable->select();
$artists->buffer();
foreach ($artists as $artist) {
$albumRows = $this->albumTable->select(['artist_id' => $artist->getId()]);
$artist->setAlbums(iterator_to_array($albumRows));
}
return $artists;
}
// public function save()
// public function delete()
}
class ArtistEntity {
protected $id;
protected $name;
protected $albums;
public function setId($id) {
$this->id = $id;
}
public function getId() {
return $this->id;
}
public function setAlbums($albums) {
$this->albums = $albums;
}
public function setName($name) {
$this->name = $name;
}
public function getAlbums() {
return $this->albums;
}
public function getName() {
return $this->name;
}
}
class AlbumEntity {
protected $title;
protected $releaseDate;
public function setReleaseDate($releaseDate) {
$this->releaseDate = $releaseDate;
}
public function getReleaseDate() {
return $this->releaseDate;
}
public function setTitle($title) {
$this->title = $title;
}
public function getTitle() {
return $this->title;
}
}
}
// execution
namespace {
$controller = new Application\Controller\ArtistController(
new Application\Model\ArtistRepository($db)
);
$controller->showArtistsAction();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment