X-Git-Url: http://git.inspyration.org/?p=zf2.biz%2Fgalerie.git;a=blobdiff_plain;f=vendor%2Fzf2biz%2FCustom%2FModel%2FEntityManager.php;fp=vendor%2Fzf2biz%2FCustom%2FModel%2FEntityManager.php;h=62fc9e9e9ccf1aafb05cfa881450ed05815da17f;hp=0000000000000000000000000000000000000000;hb=624ee4b92cbbcdfaf2fbedf742c5732f1eb05902;hpb=fb4b054068ef44c4031b88313636619641c8ee60 diff --git a/vendor/zf2biz/Custom/Model/EntityManager.php b/vendor/zf2biz/Custom/Model/EntityManager.php new file mode 100644 index 0000000..62fc9e9 --- /dev/null +++ b/vendor/zf2biz/Custom/Model/EntityManager.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($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. + +}