Skip to content

Instantly share code, notes, and snippets.

@fwahlqvist
Forked from jasongrimes/AlbumController.php
Created April 29, 2012 11:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fwahlqvist/2549574 to your computer and use it in GitHub Desktop.
Save fwahlqvist/2549574 to your computer and use it in GitHub Desktop.
Extension of Akrabat's ZF2 tutorial adding support for Doctrine.
<?php
namespace Album\Controller;
use Zend\Mvc\Controller\ActionController,
Album\Form\AlbumForm,
Album\Entity\Album,
Doctrine\ORM\EntityManager;
class AlbumController extends ActionController
{
/**
* @var \Doctrine\ORM\EntityManager
*/
protected $_em;
public function setEntityManager(EntityManager $em)
{
$this->_em = $em;
}
public function indexAction()
{
return array(
'albums' => $this->_em->getRepository('Album\Entity\Album')->findAll(),
);
}
public function addAction()
{
$form = new AlbumForm();
$form->submit->setLabel('Add');
$request = $this->getRequest();
if ($request->isPost()) {
$formData = $request->post()->toArray();
if ($form->isValid($formData)) {
$album = new Album();
$album->artist = $form->getValue('artist');
$album->title = $form->getValue('title');
$this->_em->persist($album);
$this->_em->flush();
// Redirect to list of albums
return $this->redirect()->toRoute('default', array(
'controller' => 'album',
'action' => 'index',
));
}
}
return array('form' => $form);
}
public function editAction()
{
$form = new AlbumForm();
$form->submit->setLabel('Edit');
$request = $this->getRequest();
if ($request->isPost()) {
$formData = $request->post()->toArray();
if ($form->isValid($formData)) {
$album = $this->_em->find('Album\Entity\Album', $form->getValue('id'));
if ($album) {
$album->artist = $form->getValue('artist');
$album->title = $form->getValue('title');
$this->_em->flush();
}
// Redirect to list of albums
return $this->redirect()->toRoute('default', array(
'controller' => 'album',
'action' => 'index',
));
}
} else {
$album = $this->_em->find('Album\Entity\Album', $request->query()->get('id', 0));
if ($album) {
$form->populate($album->toArray());
}
}
return array('form' => $form);
}
public function deleteAction()
{
$request = $this->getRequest();
if ($request->isPost()) {
$del = $request->post()->get('del', 'No');
if ($del == 'Yes') {
$album = $this->_em->find('Album\Entity\Album', $request->post()->get('id'));
if ($album) {
$this->_em->remove($album);
$this->_em->flush();
}
}
// Redirect to list of albums
return $this->redirect()->toRoute('default', array(
'controller' => 'album',
'action' => 'index',
));
}
$id = $request->query()->get('id', 0);
return array('album' => $this->_em->find('Album\Entity\Album', $id)->toArray());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment