Ajout du modèle GalerieInfo
[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
9 use Custom\Model\Entity;
10
11 class GalerieInfoTable implements TableGatewayInterface
12 {
13
14     protected $adapter;
15     protected $resultSetPrototype;
16     protected $sql;
17
18     public function __construct(Adapter $adapter) {
19         // Gestion de l'adaptateur
20         if (!$adapter instanceof Adapter) {
21             throw new Exception\RuntimeException('GalerieInfoTable does not have an valid Adapter parameter');
22         }
23         $this->adapter = $adapter;
24
25         // Utilisation du patron de conception Prototype
26         // pour la création des objets ResultSet
27         $this->resultSetPrototype = new ResultSet();
28         $this->resultSetPrototype->setArrayObjectPrototype(
29             new GalerieInfo()
30         );
31
32         // Initialisation de l'outil de création de requête
33         $this->sql = new Sql($this->adapter, $this->getTable());
34     }
35
36
37     public function getTable()
38     {
39         return 'gallery'; // Table centrale de la requête
40     }
41
42
43     public function select($where = null)
44     {
45         $select = $this->sql->select()
46             ->columns(array('name', 'description'))
47             ->join('user', 'gallery.id_user = user.id', array(
48                 'username' => new \Zend\Db\Sql\Expression("user.firstname || ' ' || user.lastname")
49             ))
50             ->join('photo', 'gallery.id = photo.id_gallery', array(
51                 'nb' => new \Zend\Db\Sql\Expression('count(photo.id)')
52             ))
53             ->group(array(
54                 'user.lastname',
55                 'user.firstname',
56                 'gallery.name'
57             ))
58             ->order(array(
59                 'user.lastname',
60                 'user.firstname',
61                 'gallery.name'
62             ));
63         if ($where) {
64             $select->where($where);
65         }
66
67         // prepare and execute
68         $statement = $this->sql->prepareStatementForSqlObject($select);
69         $result = $statement->execute();
70
71         // build result set
72         $resultSet = clone $this->resultSetPrototype;
73         $resultSet->initialize($result);
74
75         return $resultSet;
76     }
77
78     public function insert($set) {
79         throw new \Exception('insert is not allowed');
80     }
81
82     public function update($set, $where = null) {
83         throw new \Exception('update is not allowed');
84     }
85
86     public function delete($where) {
87         throw new \Exception('delete is not allowed');
88     }
89
90 }