Permalink
Browse files

Initial commit of ZF2 doctrine tutorial

  • Loading branch information...
1 parent 55d1ab3 commit c5c92a9349c8a778f82667ca72e36b7401771226 @jasongrimes committed Feb 11, 2012
View
@@ -0,0 +1,2 @@
+# Ignore third-party modules
+/vendor
View
@@ -1,42 +1,5 @@
-ZendSkeletonApplication
-=======================
+Sample application using Zend Framework 2 and Doctrine 2. Based on
+Rob "Akrabat" Allen's ZF2 tutorial. Requires Zend Framework and DoctrineORM modules.
-Introduction
-------------
-This is a simple, skeleton application using the ZF2 MVC layer and module
-systems. This application is meant to be used as a starting place for those
-looking to get their feet wet with ZF2.
+See http://www.jasongrimes.org/2012/01/using-doctrine-2-in-zend-framework-2
-
-Installation
-------------
-The easiest way to get a working copy of this project is to do a recursive
-clone:
-
- git clone --recursive git://github.com/zendframework/ZendSkeletonApplication.git
-
-After the clone is complete, set up a virtual host to point to the public/
-directory of the project and you should be ready to go!
-
-If you're wondering what the `--recursive` flag is, keep reading:
-
-Git Submodules
---------------
-This project makes use of [Git submodules](http://book.git-scm.com/5_submodules.html).
-Utilizing Git submodules allows us to reference an exact commit in the upstream
-[zendframework/zf2](https://github.com/zendframework/zf2) repository and ensure
-that those who have cloned the project have that same commit checked out. This
-provides several benefits:
-
-* Developers do not have to worry about which commit of the zf2 project to have
- checked out for this project to work.
-* No additional steps to "install" Zend Framework are needed; it "just works"
- after a cloning the project.
-
-There are a couple of mild caveats to be aware of:
-
-* Be sure to always run `git submodule update` after pulling, as merge/rebase
- does not automatically update the checked out commit in submodules if it has
- been changed.
-* The initial clone will be a bit slower, due to it having to pull down a
- separate copy of ZF2 from what you already have.
@@ -2,6 +2,9 @@
return array(
'modules' => array(
'Application',
+ 'Album',
+ 'DoctrineModule',
+ 'DoctrineORMModule',
),
'module_listener_options' => array(
'config_cache_enabled' => false,
@@ -0,0 +1,99 @@
+ <?php
+ /**
+ * Doctrine ORM Configuration
+ *
+ * If you have a ./configs/autoload/ directory set up for your project, you can
+ * drop this config file in it and change the values as you wish. This file is intended
+ * to be used with a standard Doctrine ORM setup. If you have something more advanced
+ * you may override the Zend\Di configuration manually (see module.config.php).
+ */
+$settings = array(
+ // if disabled will not register annotations
+ 'use_annotations' => true,
+
+ // if use_annotations (above) is set to true this file will be registered
+ 'annotation_file' => __DIR__ . '/../../vendor/DoctrineORMModule/vendor/doctrine-orm/lib/Doctrine/ORM/Mapping/Driver/DoctrineAnnotations.php',
+
+ // enables production mode by disabling generation of proxies
+ 'production' => false,
+
+ // sets the cache to use for metadata: one of 'array', 'apc', or 'memcache'
+ 'cache' => 'array',
+
+ // only used if cache is set to memcache
+ 'memcache' => array(
+ 'host' => '127.0.0.1',
+ 'port' => '11211'
+ ),
+
+ // connection parameters
+ 'connection' => array(
+ 'driver' => 'pdo_mysql',
+ 'host' => 'localhost',
+ 'port' => '3306',
+ 'user' => 'root',
+ 'password' => 'dev',
+ 'dbname' => 'zftutorial',
+ ),
+
+ // driver settings
+ 'driver' => array(
+ 'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
+ 'namespace' => 'Application\Entity',
+ 'paths' => array('module/Application/src/Application/Entity')
+ ),
+
+ // namespace aliases for annotations
+ 'namespace_aliases' => array(
+ ),
+);
+
+/**
+ * YOU DO NOT NEED TO EDIT BELOW THIS LINE.
+ */
+$cache = array('array', 'memcache', 'apc');
+if (!in_array($settings['cache'], $cache)) {
+ throw new InvalidArgumentException(sprintf(
+ 'cache must be one of: %s',
+ implode(', ', $cache)
+ ));
+}
+$settings['cache'] = 'doctrine_cache_' . $settings['cache'];
+
+return array(
+ 'doctrine_orm_module' => array(
+ 'annotation_file' => $settings['annotation_file'],
+ 'use_annotations' => $settings['use_annotations'],
+ ),
+ 'di' => array(
+ 'instance' => array(
+ 'doctrine_memcache' => array(
+ 'parameters' => $settings['memcache']
+ ),
+ 'orm_config' => array(
+ 'parameters' => array(
+ 'opts' => array(
+ 'entity_namespaces' => $settings['namespace_aliases'],
+ 'auto_generate_proxies' => !$settings['production']
+ ),
+ 'metadataCache' => $settings['cache'],
+ 'queryCache' => $settings['cache'],
+ 'resultCache' => $settings['cache'],
+ )
+ ),
+ 'orm_connection' => array(
+ 'parameters' => array(
+ 'params' => $settings['connection']
+ ),
+ ),
+ 'orm_driver_chain' => array(
+ 'parameters' => array(
+ 'drivers' => array(
+ 'application_annotation_driver' => $settings['driver']
+ ),
+ 'cache' => $settings['cache']
+ )
+ ),
+ ),
+ ),
+);
View
@@ -0,0 +1,27 @@
+<?php
+
+namespace Album;
+
+use Zend\Module\Consumer\AutoloaderProvider;
+
+class Module implements AutoloaderProvider
+{
+ public function getAutoloaderConfig()
+ {
+ return array(
+ 'Zend\Loader\ClassMapAutoloader' => array(
+ __DIR__ . '/autoload_classmap.php',
+ ),
+ 'Zend\Loader\StandardAutoloader' => array(
+ 'namespaces' => array(
+ __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
+ ),
+ ),
+ );
+ }
+
+ public function getConfig()
+ {
+ return include __DIR__ . '/config/module.config.php';
+ }
+}
@@ -0,0 +1,2 @@
+<?php
+return array();
@@ -0,0 +1,56 @@
+<?php
+
+namespace Album;
+
+return array(
+ 'di' => array(
+ 'instance' => array(
+ 'alias' => array(
+ 'album' => 'Album\Controller\AlbumController',
+ ),
+ 'Album\Controller\AlbumController' => array(
+ 'parameters' => array(
+ // 'albumTable' => 'Album\Model\AlbumTable',
+ 'em' => 'doctrine_em',
+ ),
+ ),
+ 'Album\Model\AlbumTable' => array(
+ 'parameters' => array(
+ 'config' => 'Zend\Db\Adapter\Mysqli',
+ ),
+ ),
+ 'Zend\Db\Adapter\Mysqli' => array(
+ 'parameters' => array(
+ 'config' => array(
+ 'host' => 'localhost',
+ 'username' => 'root',
+ 'password' => 'dev',
+ 'dbname' => 'zftutorial',
+ ),
+ ),
+ ),
+ 'Zend\View\PhpRenderer' => array(
+ 'parameters' => array(
+ 'options' => array(
+ 'script_paths' => array(
+ 'album' => __DIR__ . '/../views',
+ ),
+ ),
+ ),
+ ),
+ 'orm_driver_chain' => array(
+ 'parameters' => array(
+ 'drivers' => array(
+ 'Album' => array(
+ 'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
+ 'namespace' => __NAMESPACE__ . '\Entity',
+ 'paths' => array(
+ __DIR__ . '/../src/' . __NAMESPACE__ . '/Entity'
+ ),
+ ),
+ ),
+ ),
+ ),
+ ),
+ ),
+);
@@ -0,0 +1,111 @@
+<?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());
+ }
+}
Oops, something went wrong.

0 comments on commit c5c92a9

Please sign in to comment.