2 namespace Custom\Model;
4 use Zend\Db\Adapter\Adapter;
5 use Zend\Db\ResultSet\ResultSet;
6 use Zend\Db\TableGateway\AbstractTableGateway;
8 abstract class Manager extends AbstractTableGateway
13 public function __construct(
17 // Composition avec l'adaptateur
18 $this->adapter = $adapter;
20 // Composition avec l'entité
21 $this->entity = $entity;
23 // Utilisation du patron de conception Prototype
24 // pour la création des objets ResultSet
25 $this->resultSetPrototype = new ResultSet();
26 $this->resultSetPrototype->setArrayObjectPrototype(
30 // Initialisation du gestionnaire
36 return $this->select();
39 public function one($primary_array=array())
41 if (!count($primary_array)) {
45 foreach($primary_array as $p) {
53 $row = $this->select($primary_array)->current();
58 foreach($primary_array as $k => $v) {
59 $keys[] = "{$k}: {$v}";
61 $keys = implode(', ', $keys);
62 throw new \Exception("cannot get row {{$keys}} in table 'galerie'");
67 public function any($primary_array)
69 if (!count($primary_array)) {
73 foreach($primary_array as $p) {
81 $row = $this->select($primary_array)->current();
87 protected abstract function is_new(Entity $entity);
88 protected abstract function extract_primary(Entity $entity);
90 public function save(Entity $entity)
92 if ($this->is_new()) {
94 $entity->toUpdatableArray()
96 } elseif ($this->getGalerie(extract_primary($entity))) {
98 $entity->toUpdatableArray(),
99 $entity->toPrimaryArray()
103 foreach($primary_array as $k => $v) {
104 $keys[] = "{$k}: {$v}";
106 $keys = implode(', ', $keys);
107 throw new \Exception("cannot update row {{$keys}} in table 'galerie'");
111 // La fonction delete du père suffit à notre besoin.