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

index 9804a5e..3e3843d 100644 (file)
@@ -12,6 +12,7 @@ use Zend\Mvc\ModuleRouteListener;
 
 use Galerie\Model\GalerieArrayTable;
 use Galerie\Model\GalerieATable;
+use Galerie\Model\GalerieBTable;
 
 class Module implements
     AutoloaderProviderInterface,
@@ -58,6 +59,11 @@ class Module implements
                         $sm->get('Zend\Db\Adapter\Adapter')
                     );
                 },
+                'Galerie\Model\GalerieBTable' => function($sm) {
+                    return new GalerieBTable(
+                        $sm->get('Zend\Db\Adapter\Adapter')
+                    );
+                },
             ),
         );
     }
index c5389b1..de82f92 100644 (file)
@@ -29,6 +29,15 @@ class IndexController extends AbstractActionController
         return $this->_galerieATable;
     }
 
+    private function _getGalerieBTable()
+    {
+        if (!$this->_galerieBTable) {
+            $sm = $this->getServiceLocator();
+            $this->_galerieBTable = $sm->get('Galerie\Model\GalerieBTable');
+        }
+        return $this->_galerieBTable;
+    }
+
     public function indexAction() 
     { 
         return new ViewModel(array(
@@ -36,6 +45,8 @@ class IndexController extends AbstractActionController
             'GalerieArray_one' => $this->_getGalerieArrayTable()->getGalerie(1),
             'GalerieA_all' => $this->_getGalerieATable()->fetchAll(),
             'GalerieA_one' => $this->_getGalerieATable()->getGalerie(1),
+            'GalerieB_all' => $this->_getGalerieBTable()->fetchAll(),
+            'GalerieB_one' => $this->_getGalerieBTable()->getGalerie(1),
         )); 
     } 
 
diff --git a/module/Galerie/src/Galerie/Model/GalerieB.php b/module/Galerie/src/Galerie/Model/GalerieB.php
new file mode 100644 (file)
index 0000000..bf3507a
--- /dev/null
@@ -0,0 +1,104 @@
+<?php
+
+namespace Galerie\Model;
+
+class GalerieB
+{
+    protected $id;
+    protected $id_user;
+    protected $name;
+    protected $description;
+    protected $created;
+    protected $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,
+        );
+    }
+
+
+    public function getId()
+    {
+        return $this->id;
+    }
+
+    public function setId($id)
+    {
+        $this->id = $id;
+        return $this;
+    }
+
+    public function getIdUser()
+    {
+        return $this->id_user;
+    }
+
+    public function setIdUser($id_user)
+    {
+        $this->id_user = $id_user;
+        return $this;
+    }
+
+    public function getName()
+    {
+        return $this->name;
+    }
+
+    public function setName($name)
+    {
+        $this->name = $name;
+        return $this;
+    }
+
+    public function getDescription()
+    {
+        return $this->description;
+    }
+
+    public function setDescription ($description)
+    {
+        $this-> description = $description;
+        return $this;
+    }
+
+    public function getCreated()
+    {
+        return $this->created;
+    }
+
+    public function setCreated($created)
+    {
+        $this->created = $created;
+        return $this;
+    }
+
+    public function getUpdated()
+    {
+        return $this->updated;
+    }
+
+    public function setUpdated($updated)
+    {
+        $this->updated = $updated;
+        return $this;
+    }
+
+}
diff --git a/module/Galerie/src/Galerie/Model/GalerieBTable.php b/module/Galerie/src/Galerie/Model/GalerieBTable.php
new file mode 100644 (file)
index 0000000..6f8a3d8
--- /dev/null
@@ -0,0 +1,82 @@
+<?php
+namespace Galerie\Model;
+
+use Zend\Db\Adapter\Adapter;
+use Zend\Db\ResultSet\ResultSet;
+use Zend\Db\TableGateway\AbstractTableGateway;
+
+
+class GalerieBTable 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 GalerieB()
+        );
+
+        // 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(GalerieA $galerie)
+    {
+        if ($galerie->getId() === null) {
+            $this->insert(
+                array(
+                    'id_user' => $galerie->getName(),
+                    'name' => $galerie->getName(),
+                    'description' => $galerie->getDescription(),
+                )
+            );
+        } elseif ($this->getGalerie($galerie->getId())) {
+            $this->update(
+                array(
+                    'name' => $galerie->getName(),
+                    'description' => $galerie->getDescription(),
+                ),
+                array(
+                    'id' => $galerie->getId(),
+                )
+            );
+        } else {
+            throw new \Exception("cannot update row {$galerie->getId()} in table 'galerie'");
+        }
+    }
+
+    public function deleteGalerie($id)
+    {
+        $this->delete(array(
+                'id' => (int) $id
+            )
+        );
+    }
+
+}
index 096de70..254f478 100644 (file)
@@ -1,24 +1,37 @@
 <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>
+
+<h1>GalerieArray</h1>
+<h2>test de fetchAll()</h2>
 
 <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>
+<h2>test de getGalerie</h2>
 
 <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>
+<h1>GalerieA</h1>
+<h2>test de fetchAll()</h2>
 
 <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>
+<h2>test de getGalerie</h2>
 
 <pre><?php print_r($GalerieA_one); ?></pre>
 <p>Identifiant&nbsp;: <?php echo $GalerieA_one->id; ?></p>
+
+
+<h1>GalerieB</h1>
+<h2>test de fetchAll()</h2>
+
+<pre><?php echo $GalerieB_all->count(); ?></pre>
+<pre><?php print_r($GalerieB_all->toArray()); ?></pre>
+
+<h2>test de getGalerie</h2>
+
+<pre><?php print_r($GalerieB_one); ?></pre>
+<p>Identifiant&nbsp;: <?php echo $GalerieB_one->getId(); ?></p>