2 namespace Galerie\Model;
4 use Zend\Db\Adapter\Adapter;
5 use Zend\Db\ResultSet\ResultSet;
6 use Zend\Db\TableGateway\TableGatewayInterface;
9 use Zend\Db\Sql\Select;
11 use Custom\Model\Entity;
13 class GalerieInfoTable implements TableGatewayInterface
17 protected $resultSetPrototype;
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');
25 $this->adapter = $adapter;
27 // Utilisation du patron de conception Prototype
28 // pour la création des objets ResultSet
29 $this->resultSetPrototype = new ResultSet();
30 $this->resultSetPrototype->setArrayObjectPrototype(
34 // Initialisation de l'outil de création de requête
35 $this->sql = new Sql($this->adapter, $this->getTable());
39 public function getTable()
41 return 'gallery'; // Table centrale de la requête
45 public function select($where = null, $order = null, $limit = null, $offset = null)
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")
52 ->join('photo', 'gallery.id = photo.id_gallery', array(
53 'nb' => new \Zend\Db\Sql\Expression('count(photo.id)')
66 $select->where($where);
69 $select->order($order);
72 $select->limit($limit);
75 $select->offset($offset);
78 // prepare and execute
79 $statement = $this->sql->prepareStatementForSqlObject($select);
80 $result = $statement->execute();
83 $resultSet = clone $this->resultSetPrototype;
84 $resultSet->initialize($result);
89 public function insert($set) {
90 throw new \Exception('insert is not allowed');
93 public function update($set, $where = null) {
94 throw new \Exception('update is not allowed');
97 public function delete($where) {
98 throw new \Exception('delete is not allowed');
101 public function all()
103 return $this->select();
106 public function one($id)
111 $row = $this->select(array('gallery.id' => (int) $id))->current();
114 throw new \Exception("cannot get row {id: {$id}} in table 'galerie'");
119 public function any($id)
124 $row = $this->select(array('gallery.id' => (int) $id))->current();
129 public function all_by_user($id_user)
131 return $this->select(array('gallery.id_user' => (int) $id_user));
137 public function count_all()
139 $select = $this->sql->select()->columns(array(
140 'nb' => new \Zend\Db\Sql\Expression('count(gallery.id)')
143 // prepare and execute
144 $statement = $this->sql->prepareStatementForSqlObject($select);
145 $result = $statement->execute()->current();
147 return $result['nb'];
150 public function getPartial($start, $length, $tri, $senstri, $filtre)
153 $where->like('gallery.name', "%{$filtre}%");
155 $where->like('gallery.description', "%{$filtre}%");
157 return $this->select($where, "{$tri} {$senstri}", $length, $start);
161 public function csvHeader()
163 return "Id;Nom;Description;Propriétaire;Nombre";