2 namespace Galerie\Model;
4 use Zend\Db\Adapter\Adapter;
5 use Zend\Db\ResultSet\ResultSet;
6 use Zend\Db\TableGateway\TableGatewayInterface;
10 use Custom\Model\Entity;
12 class GalerieInfoTable implements TableGatewayInterface
16 protected $resultSetPrototype;
19 public function __construct(Adapter $adapter) {
20 // Gestion de l'adaptateur
21 if (!$adapter instanceof Adapter) {
22 throw new Exception\RuntimeException('GalerieInfoTable does not have an valid Adapter parameter');
24 $this->adapter = $adapter;
26 // Utilisation du patron de conception Prototype
27 // pour la création des objets ResultSet
28 $this->resultSetPrototype = new ResultSet();
29 $this->resultSetPrototype->setArrayObjectPrototype(
33 // Initialisation de l'outil de création de requête
34 $this->sql = new Sql($this->adapter, $this->getTable());
38 public function getTable()
40 return 'gallery'; // Table centrale de la requête
44 public function select($where = null, $order = null, $limit = null, $offset = null)
46 $select = $this->sql->select()
47 ->columns(array('id', 'name', 'description'))
48 ->join('user', 'gallery.id_user = user.id', array(
49 'username' => new \Zend\Db\Sql\Expression("user.firstname || ' ' || user.lastname")
51 ->join('photo', 'gallery.id = photo.id_gallery', array(
52 'nb' => new \Zend\Db\Sql\Expression('count(photo.id)')
65 $select->where($where);
68 $select->order($order);
71 $select->limit($limit);
74 $select->offset($offset);
77 // prepare and execute
78 $statement = $this->sql->prepareStatementForSqlObject($select);
79 $result = $statement->execute();
82 $resultSet = clone $this->resultSetPrototype;
83 $resultSet->initialize($result);
88 public function insert($set) {
89 throw new \Exception('insert is not allowed');
92 public function update($set, $where = null) {
93 throw new \Exception('update is not allowed');
96 public function delete($where) {
97 throw new \Exception('delete is not allowed');
100 public function all()
102 return $this->select();
105 public function one($id)
110 $row = $this->select(array('gallery.id' => (int) $id))->current();
113 throw new \Exception("cannot get row {id: {$id}} in table 'galerie'");
118 public function any($id)
123 $row = $this->select(array('gallery.id' => (int) $id))->current();
128 public function all_by_user($id_user)
130 return $this->select(array('gallery.id_user' => (int) $id_user));
136 public function count_all()
138 $select = $this->sql->select()->columns(array(
139 'nb' => new \Zend\Db\Sql\Expression('count(gallery.id)')
142 // prepare and execute
143 $statement = $this->sql->prepareStatementForSqlObject($select);
144 $result = $statement->execute()->current();
146 return $result['nb'];
149 public function getPartial($start, $length, $tri, $senstri, $filtre)
152 $where->like('gallery.name', "%{$filtre}%");
154 $where->like('gallery.description', "%{$filtre}%");
156 return $this->select($where, "{$tri} {$senstri}", $length, $start);