Rajout d'une paire + correction ajout
[zf2.biz/galerie.git] / module / Galerie / src / Galerie / Model / GalerieInfoTable.php
1 <?php
2 namespace Galerie\Model;
3
4 use Zend\Db\Adapter\Adapter;
5 use Zend\Db\ResultSet\ResultSet;
6 use Zend\Db\TableGateway\TableGatewayInterface;
7 use Zend\Db\Sql\Sql;
8 use Zend\Db\Sql\Where;
9 use Zend\Db\Sql\Select;
10
11 use Custom\Model\Entity;
12
13 class GalerieInfoTable implements TableGatewayInterface
14 {
15
16     protected $adapter;
17     protected $resultSetPrototype;
18     protected $sql;
19
20     public function __construct(Adapter $adapter) {
21         // Gestion de l'adaptateur
22         if (!$adapter instanceof Adapter) {
23             throw new Exception\RuntimeException('GalerieInfoTable does not have an valid Adapter parameter');
24         }
25         $this->adapter = $adapter;
26
27         // Utilisation du patron de conception Prototype
28         // pour la création des objets ResultSet
29         $this->resultSetPrototype = new ResultSet();
30         $this->resultSetPrototype->setArrayObjectPrototype(
31             new GalerieInfo()
32         );
33
34         // Initialisation de l'outil de création de requête
35         $this->sql = new Sql($this->adapter, $this->getTable());
36     }
37
38
39     public function getTable()
40     {
41         return 'gallery'; // Table centrale de la requête
42     }
43
44
45     public function select($where = null, $order = null, $limit = null, $offset = null)
46     {
47         $select = $this->sql->select()
48             ->columns(array('id', 'name', 'description'))
49             ->join('user', 'gallery.id_user = user.id', array(
50                 'username' => new \Zend\Db\Sql\Expression("user.firstname || ' ' || user.lastname")
51             ))
52             ->join('photo', 'gallery.id = photo.id_gallery', array(
53                 'nb' => new \Zend\Db\Sql\Expression('count(photo.id)')
54             ), Select::JOIN_LEFT)
55             ->group(array(
56                 'user.lastname',
57                 'user.firstname',
58                 'gallery.name'
59             ))
60             ->order(array(
61                 'user.lastname',
62                 'user.firstname',
63                 'gallery.name'
64             ));
65         if ($where) {
66             $select->where($where);
67         }
68         if ($order) {
69             $select->order($order);
70         }
71         if ($limit) {
72             $select->limit($limit);
73         }
74         if ($offset) {
75             $select->offset($offset);
76         }
77
78         // prepare and execute
79         $statement = $this->sql->prepareStatementForSqlObject($select);
80         $result = $statement->execute();
81
82         // build result set
83         $resultSet = clone $this->resultSetPrototype;
84         $resultSet->initialize($result);
85
86         return $resultSet;
87     }
88
89     public function insert($set) {
90         throw new \Exception('insert is not allowed');
91     }
92
93     public function update($set, $where = null) {
94         throw new \Exception('update is not allowed');
95     }
96
97     public function delete($where) {
98         throw new \Exception('delete is not allowed');
99     }
100
101     public function all()
102     {
103         return $this->select();
104     }
105
106     public function one($id)
107     {
108         if ($id === null) {
109             $row = null;
110         } else {
111             $row = $this->select(array('gallery.id' => (int) $id))->current();
112         }
113         if (!$row) {
114             throw new \Exception("cannot get row {id: {$id}} in table 'galerie'");
115         }
116         return $row;
117     }
118
119     public function any($id)
120     {
121         if ($id === null) {
122             $row = null;
123         } else {
124             $row = $this->select(array('gallery.id' => (int) $id))->current();
125         }
126         return $row;
127     }
128
129     public function all_by_user($id_user)
130     {
131         return $this->select(array('gallery.id_user' => (int) $id_user));
132     }
133
134
135
136
137     public function count_all()
138     {
139         $select = $this->sql->select()->columns(array(
140             'nb' => new \Zend\Db\Sql\Expression('count(gallery.id)')
141         ));
142
143         // prepare and execute
144         $statement = $this->sql->prepareStatementForSqlObject($select);
145         $result = $statement->execute()->current();
146
147         return $result['nb'];
148     }
149
150     public function getPartial($start, $length, $tri, $senstri, $filtre)
151     {
152         $where = new Where;
153         $where->like('gallery.name', "%{$filtre}%");
154         $where->or;
155         $where->like('gallery.description', "%{$filtre}%"); 
156
157         return $this->select($where, "{$tri} {$senstri}", $length, $start);
158     }
159
160
161     public function csvHeader()
162     {
163         return "Id;Nom;Description;Propriétaire;Nombre";
164     }
165
166
167 }