From 8ce32d13a3301c30a9e7bc7a17985ccfd6415b96 Mon Sep 17 00:00:00 2001 From: =?utf8?q?S=C3=A9bastien=20CHAZALLET?= Date: Wed, 7 Nov 2012 00:45:39 +0100 Subject: [PATCH] =?utf8?q?Rajout=20d'un=20Gestionnaire=20de=20mod=C3=A8le=20?= =?utf8?q?g=C3=A9n=C3=A9rique?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- vendor/zf2biz/Custom/Model/Manager.php | 113 ++++++++++++++++++++++++++++ vendor/zf2biz/Custom/autoload_classmap.php | 3 +- 2 files changed, 115 insertions(+), 1 deletion(-) create mode 100644 vendor/zf2biz/Custom/Model/Manager.php diff --git a/vendor/zf2biz/Custom/Model/Manager.php b/vendor/zf2biz/Custom/Model/Manager.php new file mode 100644 index 0000000..e7ae619 --- /dev/null +++ b/vendor/zf2biz/Custom/Model/Manager.php @@ -0,0 +1,113 @@ +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/autoload_classmap.php b/vendor/zf2biz/Custom/autoload_classmap.php index e7ebc58..2dea9d0 100644 --- a/vendor/zf2biz/Custom/autoload_classmap.php +++ b/vendor/zf2biz/Custom/autoload_classmap.php @@ -1,5 +1,6 @@ __DIR__ . '/Model/Entity.php', + 'Custom\Model\Entity' => __DIR__ . '/Model/Entity.php', + 'Custom\Model\Manager' => __DIR__ . '/Model/Manager.php', ); -- 1.7.10.4