Rajout de la gestion des paires pour les select
authorSébastien CHAZALLET <s.chazallet@gmail.com>
Fri, 30 Nov 2012 10:16:35 +0000 (11:16 +0100)
committerSébastien CHAZALLET <s.chazallet@gmail.com>
Fri, 30 Nov 2012 10:16:35 +0000 (11:16 +0100)
vendor/zf2biz/Custom/Model/EntityManager.php [new file with mode: 0644]
vendor/zf2biz/Custom/Model/Manager.php [deleted file]
vendor/zf2biz/Custom/Model/Pair.php [new file with mode: 0644]
vendor/zf2biz/Custom/Model/PairFeature.php [new file with mode: 0644]
vendor/zf2biz/Custom/Model/PairManager.php [new file with mode: 0644]

diff --git a/vendor/zf2biz/Custom/Model/EntityManager.php b/vendor/zf2biz/Custom/Model/EntityManager.php
new file mode 100644 (file)
index 0000000..62fc9e9
--- /dev/null
@@ -0,0 +1,113 @@
+<?php
+namespace Custom\Model;
+
+use Zend\Db\Adapter\Adapter;
+use Zend\Db\ResultSet\ResultSet;
+use Zend\Db\TableGateway\AbstractTableGateway;
+
+abstract class EntityManager extends AbstractTableGateway
+{
+
+    protected $entity;
+
+    public function __construct(
+        Adapter $adapter,
+        Entity $entity
+    ) {
+        // Composition avec l'adaptateur
+        $this->adapter = $adapter;
+
+        // Composition avec l'entité
+        $this->entity = $entity;
+
+        // Utilisation du patron de conception Prototype
+        // pour la création des objets ResultSet
+        $this->resultSetPrototype = new ResultSet();
+        $this->resultSetPrototype->setArrayObjectPrototype(
+            $entity
+        );
+
+        // Initialisation du gestionnaire
+        $this->initialize();
+    }
+
+    public function all()
+    {
+        return $this->select();
+    }
+
+    public function one($primary_array=array())
+    {
+        if (!count($primary_array)) {
+            $row = null;
+        } else {
+            $valid = true;
+            foreach($primary_array as $p) {
+                if ($p === null) {
+                    $row = null;
+                    $valid = false;
+                    break;
+                }
+            }
+            if ($valid) {
+                $row = $this->select($primary_array)->current();
+            }
+        }
+        if (!$row) {
+            $keys = array();
+            foreach($primary_array as $k => $v) {
+                $keys[] = "{$k}: {$v}";
+            }
+            $keys = implode(', ', $keys);
+            throw new \Exception("cannot get row {{$keys}} in table 'galerie'");
+        }
+        return $row;
+    }
+
+    public function any($primary_array)
+    {
+        if (!count($primary_array)) {
+            $row = null;
+        } else {
+            $valid = true;
+            foreach($primary_array as $p) {
+                if ($p === null) {
+                    $row = null;
+                    $valid = false;
+                    break;
+                }
+            }
+            if ($valid) {
+                $row = $this->select($primary_array)->current();
+            }
+        }
+        return $row;
+    }
+
+    protected abstract function is_new(Entity $entity);
+    protected abstract function extract_primary(Entity $entity);
+    public function save(Entity $entity)
+    {
+        if ($this->is_new($entity)) {
+            $this->insert(
+                $entity->toUpdatableArray()
+            );
+        } elseif ($this->any($this->extract_primary($entity))) {
+            $this->update(
+                $entity->toUpdatableArray(),
+                $entity->toPrimaryArray()
+            );
+        } else {
+            $keys = array();
+            foreach($primary_array as $k => $v) {
+                $keys[] = "{$k}: {$v}";
+            }
+            $keys = implode(', ', $keys);
+            throw new \Exception("cannot update row {{$keys}} in table 'galerie'");
+        }
+    }
+
+    // La fonction delete du père suffit à notre besoin.
+
+}
diff --git a/vendor/zf2biz/Custom/Model/Manager.php b/vendor/zf2biz/Custom/Model/Manager.php
deleted file mode 100644 (file)
index e7ae619..0000000
+++ /dev/null
@@ -1,113 +0,0 @@
-<?php
-namespace Custom\Model;
-
-use Zend\Db\Adapter\Adapter;
-use Zend\Db\ResultSet\ResultSet;
-use Zend\Db\TableGateway\AbstractTableGateway;
-
-abstract class Manager extends AbstractTableGateway
-{
-
-    protected $entity;
-
-    public function __construct(
-        Adapter $adapter,
-        Entity $entity
-    ) {
-        // Composition avec l'adaptateur
-        $this->adapter = $adapter;
-
-        // Composition avec l'entité
-        $this->entity = $entity;
-
-        // Utilisation du patron de conception Prototype
-        // pour la création des objets ResultSet
-        $this->resultSetPrototype = new ResultSet();
-        $this->resultSetPrototype->setArrayObjectPrototype(
-            $entity
-        );
-
-        // Initialisation du gestionnaire
-        $this->initialize();
-    }
-
-    public function all()
-    {
-        return $this->select();
-    }
-
-    public function one($primary_array=array())
-    {
-        if (!count($primary_array)) {
-            $row = null;
-        } else {
-            $valid = true;
-            foreach($primary_array as $p) {
-                if ($p === null) {
-                    $row = null;
-                    $valid = false;
-                    break;
-                }
-            }
-            if ($valid) {
-                $row = $this->select($primary_array)->current();
-            }
-        }
-        if (!$row) {
-            $keys = array();
-            foreach($primary_array as $k => $v) {
-                $keys[] = "{$k}: {$v}";
-            }
-            $keys = implode(', ', $keys);
-            throw new \Exception("cannot get row {{$keys}} in table 'galerie'");
-        }
-        return $row;
-    }
-
-    public function any($primary_array)
-    {
-        if (!count($primary_array)) {
-            $row = null;
-        } else {
-            $valid = true;
-            foreach($primary_array as $p) {
-                if ($p === null) {
-                    $row = null;
-                    $valid = false;
-                    break;
-                }
-            }
-            if ($valid) {
-                $row = $this->select($primary_array)->current();
-            }
-        }
-        return $row;
-    }
-
-    protected abstract function is_new(Entity $entity);
-    protected abstract function extract_primary(Entity $entity);
-    public function save(Entity $entity)
-    {
-        if ($this->is_new()) {
-            $this->insert(
-                $entity->toUpdatableArray()
-            );
-        } elseif ($this->getGalerie(extract_primary($entity))) {
-            $this->update(
-                $entity->toUpdatableArray(),
-                $entity->toPrimaryArray()
-            );
-        } else {
-            $keys = array();
-            foreach($primary_array as $k => $v) {
-                $keys[] = "{$k}: {$v}";
-            }
-            $keys = implode(', ', $keys);
-            throw new \Exception("cannot update row {{$keys}} in table 'galerie'");
-        }
-    }
-
-    // La fonction delete du père suffit à notre besoin.
-
-}
diff --git a/vendor/zf2biz/Custom/Model/Pair.php b/vendor/zf2biz/Custom/Model/Pair.php
new file mode 100644 (file)
index 0000000..203fe8f
--- /dev/null
@@ -0,0 +1,34 @@
+<?php
+
+namespace Custom\Model;
+
+
+class Pair
+{
+    protected static $field_id = 'id';
+    protected static $field_name = 'name';
+
+    protected $id = null;
+    protected $name = null;
+
+    public function exchangeArray($data)
+    {
+        $this->id = $data[static::$field_id];
+        $this->name = $data[static::$field_name];
+    }
+
+    public function toArray() {
+        return array(
+            'id' => $this->id,
+            'name' => $this->name,
+        );
+    }
+
+    public function getArrayCopy() {
+        return array(
+            'id' => $this->id,
+            'name' => $this->name,
+        );
+    }
+
+}
diff --git a/vendor/zf2biz/Custom/Model/PairFeature.php b/vendor/zf2biz/Custom/Model/PairFeature.php
new file mode 100644 (file)
index 0000000..10cfbac
--- /dev/null
@@ -0,0 +1,24 @@
+<?php
+
+namespace Custom\Model;
+
+use Zend\Db\TableGateway\Feature\AbstractFeature;
+
+use Zend\Db\Adapter\Driver\StatementInterface;
+use Zend\Db\Adapter\Driver\ResultInterface;
+use Zend\Db\ResultSet\ResultSetInterface;
+
+
+class PairFeature extends AbstractFeature
+{
+    public function postSelect(StatementInterface $statement, ResultInterface $result, ResultSetInterface $resultSet)
+    {
+        $result = array();
+        foreach($resultSet as $res) {
+            $r = $res->toArray();
+            $result[$r['id']] = $r['name'];
+        }
+        $this->tableGateway->setResult($result);
+    }
+
+}
diff --git a/vendor/zf2biz/Custom/Model/PairManager.php b/vendor/zf2biz/Custom/Model/PairManager.php
new file mode 100644 (file)
index 0000000..4519805
--- /dev/null
@@ -0,0 +1,56 @@
+<?php
+namespace Custom\Model;
+
+use Zend\Db\Adapter\Adapter;
+use Zend\Db\ResultSet\ResultSet;
+use Zend\Db\TableGateway\AbstractTableGateway;
+
+class PairManager extends AbstractTableGateway
+{
+
+    protected $pair;
+
+    protected $result;
+
+    public function __construct(
+        Adapter $adapter,
+        $table,
+        Pair $pair = null
+    ) {
+        // Composition avec l'adaptateur
+        $this->adapter = $adapter;
+
+        // Détermination de la table principale à requêter
+        $this->table = $table;
+
+        // Composition avec l'entité
+        if ($pair === null) {
+            $this->pair = new Pair;
+        } else {
+            $this->pair = $pair;
+        }
+
+        // Utilisation du patron de conception Prototype
+        // pour la création des objets ResultSet
+        $this->resultSetPrototype = new ResultSet();
+        $this->resultSetPrototype->setArrayObjectPrototype(
+            $this->pair
+        );
+
+        // Initialisation du gestionnaire
+        $this->initialize();
+        $this->featureSet->addFeature(new PairFeature);
+    }
+
+    public function all()
+    {
+        $this->select();
+        return $this->result;
+    }
+
+    public function setResult($result)
+    {
+        $this->result = $result;
+    }
+
+}