Rajout du modèle C
authorSébastien CHAZALLET <s.chazallet@gmail.com>
Tue, 6 Nov 2012 15:06:58 +0000 (16:06 +0100)
committerSébastien CHAZALLET <s.chazallet@gmail.com>
Tue, 6 Nov 2012 15:06:58 +0000 (16:06 +0100)
module/Galerie/Module.php
module/Galerie/src/Galerie/Controller/IndexController.php
module/Galerie/src/Galerie/Model/GalerieC.php [new file with mode: 0644]
module/Galerie/src/Galerie/Model/GalerieCTable.php [new file with mode: 0644]
module/Galerie/view/galerie/index/index.phtml

index 3e3843d..74876bb 100644 (file)
@@ -13,6 +13,7 @@ use Zend\Mvc\ModuleRouteListener;
 use Galerie\Model\GalerieArrayTable;
 use Galerie\Model\GalerieATable;
 use Galerie\Model\GalerieBTable;
+use Galerie\Model\GalerieCTable;
 
 class Module implements
     AutoloaderProviderInterface,
@@ -64,6 +65,11 @@ class Module implements
                         $sm->get('Zend\Db\Adapter\Adapter')
                     );
                 },
+                'Galerie\Model\GalerieCTable' => function($sm) {
+                    return new GalerieCTable(
+                        $sm->get('Zend\Db\Adapter\Adapter')
+                    );
+                },
             ),
         );
     }
index de82f92..7fba8bc 100644 (file)
@@ -10,6 +10,9 @@ class IndexController extends AbstractActionController
 
     private $_galerieArrayTable;
     private $_galerieATable;
+    private $_galerieBTable;
+    private $_galerieCTable;
+
 
     private function _getGalerieArrayTable()
     {
@@ -38,6 +41,15 @@ class IndexController extends AbstractActionController
         return $this->_galerieBTable;
     }
 
+    private function _getGalerieCTable()
+    {
+        if (!$this->_galerieCTable) {
+            $sm = $this->getServiceLocator();
+            $this->_galerieCTable = $sm->get('Galerie\Model\GalerieCTable');
+        }
+        return $this->_galerieCTable;
+    }
+
     public function indexAction() 
     { 
         return new ViewModel(array(
@@ -47,6 +59,8 @@ class IndexController extends AbstractActionController
             'GalerieA_one' => $this->_getGalerieATable()->getGalerie(1),
             'GalerieB_all' => $this->_getGalerieBTable()->fetchAll(),
             'GalerieB_one' => $this->_getGalerieBTable()->getGalerie(1),
+            'GalerieC_all' => $this->_getGalerieCTable()->fetchAll(),
+            'GalerieC_one' => $this->_getGalerieCTable()->getGalerie(1),
         )); 
     } 
 
diff --git a/module/Galerie/src/Galerie/Model/GalerieC.php b/module/Galerie/src/Galerie/Model/GalerieC.php
new file mode 100644 (file)
index 0000000..dcf51c7
--- /dev/null
@@ -0,0 +1,71 @@
+<?php
+
+namespace Galerie\Model;
+
+class GalerieC
+{
+    public $id;
+    public $id_user;
+    public $name;
+    public $description;
+    public $created;
+    public $updated;
+
+
+    protected $columns = array(
+        'id',
+        'id_user',
+        'name',
+        'description',
+        'created',
+        'updated',
+    );
+
+    protected $updatable_columns = array(
+        'id_user',
+        'name',
+        'description',
+        'created',
+        'updated',
+    );
+
+    protected $primary_columns = array(
+        'id',
+    );
+
+    public function exchangeArray($data, $overwrite=true)
+    {
+        foreach($this->columns as $col) {
+            if (array_key_exists($col, $data)) {
+                $this->$col = $data[$col];
+            } elseif ($overwrite) {
+                $this->$col = null;
+            }
+        }
+    }
+
+    public function toArray() {
+        $result = array();
+        foreach($this->columns as $col) {
+            $result[$col] = $this->$col;
+        }
+        return $result;
+    }
+
+    public function toUpdatableArray() {
+        $result = array();
+        foreach($this->updatable_columns as $col) {
+            $result[$col] = $this->$col;
+        }
+        return $result;
+    }
+
+    public function toPrimaryArray() {
+        $result = array();
+        foreach($this->primary_columns as $col) {
+            $result[$col] = $this->$col;
+        }
+        return $result;
+    }
+
+}
diff --git a/module/Galerie/src/Galerie/Model/GalerieCTable.php b/module/Galerie/src/Galerie/Model/GalerieCTable.php
new file mode 100644 (file)
index 0000000..92dd6ba
--- /dev/null
@@ -0,0 +1,73 @@
+<?php
+namespace Galerie\Model;
+
+use Zend\Db\Adapter\Adapter;
+use Zend\Db\ResultSet\ResultSet;
+use Zend\Db\TableGateway\AbstractTableGateway;
+
+
+class GalerieCTable 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 GalerieC()
+        );
+
+        // 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 function saveGalerie(GalerieC $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 254f478..c6d2828 100644 (file)
 
 <pre><?php print_r($GalerieB_one); ?></pre>
 <p>Identifiant&nbsp;: <?php echo $GalerieB_one->getId(); ?></p>
+
+
+<h1>GalerieC</h1>
+<h2>test de fetchAll()</h2>
+
+<pre><?php echo $GalerieC_all->count(); ?></pre>
+<pre><?php print_r($GalerieC_all->toArray()); ?></pre>
+
+<h2>test de getGalerie</h2>
+
+<pre><?php print_r($GalerieC_one); ?></pre>
+<p>Identifiant&nbsp;: <?php echo $GalerieC_one->id; ?></p>