CREATE TABLE albums (
id int(11) NOT NULL auto_increment,
artist varchar(100) NOT NULL,
title varchar(100) NOT NULL,
PRIMARY KEY (id)
);
INSERT INTO albums (artist, title) VALUES ('Coldplay', 'Mylo Xyloto');
INSERT INTO albums (artist, title) VALUES ('Noel Gallagher', 'Noel Gallagher\'s High Flying Birds!');
INSERT INTO albums (artist, title) VALUES ('Adele', '21');
INSERT INTO albums (artist, title) VALUES ('Matt Cardle', 'Letters');
INSERT INTO albums (artist, title) VALUES ('Steps', 'The Ultimate Collection');
<?php
namespace Album\Model;
use Zend\Db\Table\AbstractTable;class Albums extends AbstractTable
{
protected $_name = 'albums';
public function getAlbum($id)
{
$id = (int) $id;
$row = $this->fetchRow('id = ' . $id);
if (!$row) {
throw new Exception("Could not find row $id");
}
return $row->toArray();
}
public function addAlbum($artist, $title)
{
$data = array(
'artist' => $artist,
'title' => $title,
);
$this->insert($data);
}
public function updateAlbum($id, $artist, $title)
{
$data = array(
'artist' => $artist,
'title' => $title,
);
$this->update($data, 'id = ' . (int) $id);
}
public function deleteAlbum($id)
{
$this->delete('id =' . (int) $id);
}
}
?>
<?php
namespace Album\Controller;
use Zend\Mvc\Controller\ActionController,
Album\Model\Albums;
class AlbumController extends ActionController
{
protected $albums;
public function indexAction()
{ }
public function addAction()
{ }
public function editAction()
{ }
public function deleteAction()
{ } public function setAlbums(Albums $albums)
{
$this->albums = $albums;
return $this;
}
}
?>
<?php
$default = array(
'di' => array('instance' => array(
'alias' => array( 'album' => 'Album\Controller\AlbumController',
),
'Album\Controller\AlbumController' => array(
'parameters' => array(
'albums' => 'Album\Model\Albums',
),
),
'Album\Model\Albums' => array(
'parameters' => array(
'config' => 'Zend\Db\Adapter\Mysqli',
)),
'Zend\Db\Adapter\Mysqli' => array(
'parameters' => array(
'config' => array(
'host' => 'localhost',
'username' => 'rob',
'password' => '123456',
'dbname' => 'zf2tutorial',
),
),
),
'Zend\View\PhpRenderer' => array(
'parameters' => array(
'resolver' => 'Zend\View\TemplatePathStack',
'options' => array(
'script_paths' => array(
'Album' => __DIR__ . '/../views',
),
),
),
),
)),
);
// published environments
$production = $default;
$staging = $default;
$testing = $default;
$development = $default;
$config = compact('production', 'staging', 'testing', 'development');
return $config;
?>
<?php
'Album\Controller\AlbumController' => array(
'parameters' => array(
'albums' => 'Album\Model\Albums',
),
),
?>