From: Sébastien CHAZALLET Date: Tue, 6 Nov 2012 11:41:57 +0000 (+0100) Subject: Rajout des premiers modèles GalerieA, GalerieArray X-Git-Url: http://git.inspyration.org/?p=zf2.biz%2Fgalerie.git;a=commitdiff_plain;h=85889014266a202a509f6042a4ebe95d25533170 Rajout des premiers modèles GalerieA, GalerieArray --- diff --git a/config/autoload/global.php b/config/autoload/global.php index 104762e..39f8af7 100644 --- a/config/autoload/global.php +++ b/config/autoload/global.php @@ -10,7 +10,19 @@ * control, so do not include passwords or other sensitive information in this * file. */ - return array( - // ... + 'db' => array( + 'driver' => 'Pdo', + 'dsn' => 'sqlite:' . getcwd() . '/data/jeu_essai.db', + ), + /* Alternative + 'db' => array( + 'driver' => 'Pdo_Sqlite', + 'database' => getcwd() . '/data/jeu_essai.db', + ),*/ + 'service_manager' => array( + 'factories' => array( + 'Zend\Db\Adapter\Adapter' => 'Zend\Db\Adapter\AdapterServiceFactory', + ), + ), ); diff --git a/module/Galerie/Module.php b/module/Galerie/Module.php index 830d142..9804a5e 100644 --- a/module/Galerie/Module.php +++ b/module/Galerie/Module.php @@ -5,14 +5,19 @@ namespace Galerie; use Zend\ModuleManager\Feature\AutoloaderProviderInterface; use Zend\ModuleManager\Feature\ConfigProviderInterface; use Zend\ModuleManager\Feature\BootstrapListenerInterface; +use Zend\ModuleManager\Feature\ServiceProviderInterface; use Zend\EventManager\EventInterface; use Zend\Mvc\ModuleRouteListener; +use Galerie\Model\GalerieArrayTable; +use Galerie\Model\GalerieATable; + class Module implements AutoloaderProviderInterface, ConfigProviderInterface, - BootstrapListenerInterface + BootstrapListenerInterface, + ServiceProviderInterface { public function getAutoloaderConfig() @@ -39,4 +44,21 @@ class Module implements $e->getApplication()->getServiceManager()->get('translator'); } + public function getServiceConfig() + { + return array( + 'factories' => array( + 'Galerie\Model\GalerieArrayTable' => function($sm) { + return new GalerieArrayTable( + $sm->get('Zend\Db\Adapter\Adapter') + ); + }, + 'Galerie\Model\GalerieATable' => function($sm) { + return new GalerieATable( + $sm->get('Zend\Db\Adapter\Adapter') + ); + }, + ), + ); + } } diff --git a/module/Galerie/src/Galerie/Controller/IndexController.php b/module/Galerie/src/Galerie/Controller/IndexController.php index 48b3b61..c5389b1 100644 --- a/module/Galerie/src/Galerie/Controller/IndexController.php +++ b/module/Galerie/src/Galerie/Controller/IndexController.php @@ -3,13 +3,40 @@ namespace Galerie\Controller; use Zend\Mvc\Controller\AbstractActionController; - +use Zend\View\Model\ViewModel; class IndexController extends AbstractActionController -{ +{ + + private $_galerieArrayTable; + private $_galerieATable; + + private function _getGalerieArrayTable() + { + if (!$this->_galerieArrayTable) { + $sm = $this->getServiceLocator(); + $this->_galerieArrayTable = $sm->get('Galerie\Model\GalerieArrayTable'); + } + return $this->_galerieArrayTable; + } + + private function _getGalerieATable() + { + if (!$this->_galerieATable) { + $sm = $this->getServiceLocator(); + $this->_galerieATable = $sm->get('Galerie\Model\GalerieATable'); + } + return $this->_galerieATable; + } + public function indexAction() { - return array(); + return new ViewModel(array( + 'GalerieArray_all' => $this->_getGalerieArrayTable()->fetchAll(), + 'GalerieArray_one' => $this->_getGalerieArrayTable()->getGalerie(1), + 'GalerieA_all' => $this->_getGalerieATable()->fetchAll(), + 'GalerieA_one' => $this->_getGalerieATable()->getGalerie(1), + )); } public function editAction() diff --git a/module/Galerie/src/Galerie/Model/GalerieA.php b/module/Galerie/src/Galerie/Model/GalerieA.php new file mode 100644 index 0000000..3b11fce --- /dev/null +++ b/module/Galerie/src/Galerie/Model/GalerieA.php @@ -0,0 +1,36 @@ +id = isset($data['id']) ? $data['id'] : null; + $this->id_user = isset($data['is_user']) ? $data['id_user'] : null; + $this->name = isset($data['name']) ? $data['name'] : null; + $this->description = isset($data['description']) ? $data['description'] : null; + $this->created = isset($data['created']) ? $data['created'] : null; + $this->updated = isset($data['updated']) ? $data['updated'] : null; + } + + public function toArray($data) + { + return array( + 'id' => $this->id, + 'is_user' => $this->id_user, + 'name' => $this->name, + 'description' => $this->description, + 'created' => $this->created, + 'updated' => $this->updated, + ); + } + +} diff --git a/module/Galerie/src/Galerie/Model/GalerieATable.php b/module/Galerie/src/Galerie/Model/GalerieATable.php new file mode 100644 index 0000000..a37ce61 --- /dev/null +++ b/module/Galerie/src/Galerie/Model/GalerieATable.php @@ -0,0 +1,81 @@ +adapter = $adapter; + + // Utilisation du patron de conception Prototype + // pour la création des objets ResultSet + $this->resultSetPrototype = new ResultSet(); + $this->resultSetPrototype->setArrayObjectPrototype( + new GalerieA() + ); + + // Initialisation du gestionnaire + $this->initialize(); + } + + public function fetchAll() + { + return $this->select(); + } + + public function getGalerie($id) + { + if ($id === null) { + $row = null; + } else { + $row = $this->select(array( + 'id' => (int) $id, + ))->current(); + } + if (!$row) { + throw new \Exception("cannot get row $id in table 'galerie'"); + } + return $row; + } + +/* public saveGalerie(GalerieA $galerie) + { + if ($galerie->id === null) { + $this->insert( + array( + 'name' => $galerie->name, + 'description' => $galerie->description, + ) + ); + } elseif ($this->getGalerie($galerie->id)) { + $this->update( + array( + 'name' => $galerie->name, + 'description' => $galerie->description, + ), + array( + 'id' => $galerie->id, + ) + ); + } else { + throw new \Exception("cannot update row $galerie->id in table 'galerie'"); + } + } +*/ + public function deleteGalerie($id) + { + $this->delete(array( + 'id' => (int) $id + ) + ); + } + +} diff --git a/module/Galerie/src/Galerie/Model/GalerieArray.php b/module/Galerie/src/Galerie/Model/GalerieArray.php new file mode 100644 index 0000000..57a0263 --- /dev/null +++ b/module/Galerie/src/Galerie/Model/GalerieArray.php @@ -0,0 +1,7 @@ +adapter = $adapter; + + // Utilisation du patron de conception Prototype + // pour la création des objets ResultSet + $this->resultSetPrototype = new ResultSet(); + $this->resultSetPrototype->setArrayObjectPrototype( + new GalerieArray() + ); + + // Initialisation du gestionnaire + $this->initialize(); + } + + public function fetchAll() + { + return $this->select(); + } + + public function getGalerie($id) + { + if ($id === null) { + $row = null; + } else { + $row = $this->select(array( + 'id' => (int) $id, + ))->current(); + } + if (!$row) { + throw new \Exception("cannot get row $id in table 'galerie'"); + } + return $row; + } + +/* public saveGalerie(GalerieA $galerie) + { + if ($galerie->id === null) { + $this->insert( + array( + 'name' => $galerie->name, + 'description' => $galerie->description, + ) + ); + } elseif ($this->getGalerie($galerie->id)) { + $this->update( + array( + 'name' => $galerie->name, + 'description' => $galerie->description, + ), + array( + 'id' => $galerie->id, + ) + ); + } else { + throw new \Exception("cannot update row $galerie->id in table 'galerie'"); + } + } +*/ + public function deleteGalerie($id) + { + $this->delete(array( + 'id' => (int) $id + ) + ); + } + +} diff --git a/module/Galerie/src/Galerie/Model/GalerieE3Table.php b/module/Galerie/src/Galerie/Model/GalerieE3Table.php new file mode 100644 index 0000000..194cded --- /dev/null +++ b/module/Galerie/src/Galerie/Model/GalerieE3Table.php @@ -0,0 +1,72 @@ +adapter = $adapter; + + // Utilisation du patron de conception Prototype + // pour la création des objets ResultSet + $this->resultSetPrototype = new ResultSet(); + $this->resultSetPrototype->setArrayObjectPrototype( + new Galerie() + ); + + // Initialisation du gestionnaire + $this->initialize(); + } + + public function fetchAll() + { + return $this->select(); + } + + public function getGalerie($id) + { + if ($id === null) { + $row = null; + } else { + $row = $this->select(array( + 'id' => (int) $id + ))->current(); + } + if (!$row) { + throw new \Exception("cannot get row $id in table 'galerie'"); + } + return $row; + } + + public saveGalerie(Galerie $galerie) + { + if ($galerie->id === null) { + $this->insert( + $galerie->toUpdatableArray() + ); + } elseif ($this->getGalerie($galerie->id)) { + $this->update( + $galerie->toUpdatableArray(), + $galerie->toPrimaryArray() + ); + } else { + throw new \Exception("cannot update row $galerie->id in table 'galerie'"); + } + } + + public function deleteGalerie($id) + { + $this->delete(array( + 'id' => (int) $id + ) + ); + } + +} diff --git a/module/Galerie/view/galerie/index/index.phtml b/module/Galerie/view/galerie/index/index.phtml index ef9f43a..096de70 100644 --- a/module/Galerie/view/galerie/index/index.phtml +++ b/module/Galerie/view/galerie/index/index.phtml @@ -1 +1,24 @@

vue back-office d’une galerie : translate('index', 'galerie'); ?>

+ +

Affichage des galeries avec GalerieArray (test du fetchAll) :

+ +
count(); ?>
+
toArray()); ?>
+ +

Affichage d'une galerie avec GalerieArray (test du getGalerie) :

+ +
+

Identifiant :

+ + + + +

Affichage des galeries avec GalerieA (test du fetchAll) :

+ +
count(); ?>
+
toArray()); ?>
+ +

Affichage d'une galerie avec GalerieA (test du getGalerie) :

+ +
+

Identifiant : id; ?>