Rajout des premiers modèles GalerieA, GalerieArray
authorSébastien CHAZALLET <s.chazallet@gmail.com>
Tue, 6 Nov 2012 11:41:57 +0000 (12:41 +0100)
committerSébastien CHAZALLET <s.chazallet@gmail.com>
Tue, 6 Nov 2012 11:41:57 +0000 (12:41 +0100)
config/autoload/global.php
module/Galerie/Module.php
module/Galerie/src/Galerie/Controller/IndexController.php
module/Galerie/src/Galerie/Model/GalerieA.php [new file with mode: 0644]
module/Galerie/src/Galerie/Model/GalerieATable.php [new file with mode: 0644]
module/Galerie/src/Galerie/Model/GalerieArray.php [new file with mode: 0644]
module/Galerie/src/Galerie/Model/GalerieArrayTable.php [new file with mode: 0644]
module/Galerie/src/Galerie/Model/GalerieE3Table.php [new file with mode: 0644]
module/Galerie/view/galerie/index/index.phtml

index 104762e..39f8af7 100644 (file)
  * 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',
+        ),
+    ),
 );
index 830d142..9804a5e 100644 (file)
@@ -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')
+                    );
+                },
+            ),
+        );
+    }
 }
index 48b3b61..c5389b1 100644 (file)
@@ -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 (file)
index 0000000..3b11fce
--- /dev/null
@@ -0,0 +1,36 @@
+<?php
+
+namespace Galerie\Model;
+
+class GalerieA
+{
+    public $id;
+    public $id_user;
+    public $name;
+    public $description;
+    public $created;
+    public $updated;
+
+    public function exchangeArray($data)
+    {
+        $this->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 (file)
index 0000000..a37ce61
--- /dev/null
@@ -0,0 +1,81 @@
+<?php
+namespace Galerie\Model;
+
+use Zend\Db\Adapter\Adapter;
+use Zend\Db\ResultSet\ResultSet;
+use Zend\Db\TableGateway\AbstractTableGateway;
+
+
+class GalerieATable extends AbstractTableGateway
+{
+    public $table = 'gallery';
+
+    public function __construct(Adapter $adapter)
+    {
+        // Composition avec l'adaptateur
+        $this->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 (file)
index 0000000..57a0263
--- /dev/null
@@ -0,0 +1,7 @@
+<?php
+
+namespace Galerie\Model;
+
+class GalerieArray extends \ArrayObject
+{
+}
diff --git a/module/Galerie/src/Galerie/Model/GalerieArrayTable.php b/module/Galerie/src/Galerie/Model/GalerieArrayTable.php
new file mode 100644 (file)
index 0000000..0aecc73
--- /dev/null
@@ -0,0 +1,81 @@
+<?php
+namespace Galerie\Model;
+
+use Zend\Db\Adapter\Adapter;
+use Zend\Db\ResultSet\ResultSet;
+use Zend\Db\TableGateway\AbstractTableGateway;
+
+
+class GalerieArrayTable extends AbstractTableGateway
+{
+    public $table = 'gallery';
+
+    public function __construct(Adapter $adapter)
+    {
+        // Composition avec l'adaptateur
+        $this->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 (file)
index 0000000..194cded
--- /dev/null
@@ -0,0 +1,72 @@
+<?php
+namespace Galerie\Model
+
+use Zend\Db\Adapter\Adapter;
+use Zend\Db\ResultSet\ResultSet;
+use Zend\Db\TableGateway\AbstractTableGateway;
+
+class GalerieTableE extends AbstractTableGateway
+{
+    public $table = 'galerie';
+
+    public function __construct(Adapter $adapter)
+    {
+        // Composition avec l'adaptateur
+        $this->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
+            )
+        );
+    }
+
+}
index ef9f43a..096de70 100644 (file)
@@ -1 +1,24 @@
 <p>vue <em>back-office</em> d&rsquo;une <strong>galerie</strong>&nbsp;: <?php echo $this->translate('index', 'galerie'); ?></p>
+
+<p>Affichage des galeries avec GalerieArray (test du fetchAll)&nbsp;:</p>
+
+<pre><?php echo $GalerieArray_all->count(); ?></pre>
+<pre><?php print_r($GalerieArray_all->toArray()); ?></pre>
+
+<p>Affichage d'une galerie avec GalerieArray (test du getGalerie)&nbsp;:</p>
+
+<pre><?php print_r($GalerieArray_one); ?></pre>
+<p>Identifiant&nbsp;: <?php echo $GalerieArray_one['id']; ?></p>
+
+
+
+
+<p>Affichage des galeries avec GalerieA (test du fetchAll)&nbsp;:</p>
+
+<pre><?php echo $GalerieA_all->count(); ?></pre>
+<pre><?php print_r($GalerieA_all->toArray()); ?></pre>
+
+<p>Affichage d'une galerie avec GalerieA (test du getGalerie)&nbsp;:</p>
+
+<pre><?php print_r($GalerieA_one); ?></pre>
+<p>Identifiant&nbsp;: <?php echo $GalerieA_one->id; ?></p>