use Galerie\Model\GalerieCTable;
use Galerie\Model\GalerieCTable2;
use Galerie\Model\GalerieCTable3;
+use Galerie\Model\GalerieInfoTable;
class Module implements
$sm->get('Zend\Db\Adapter\Adapter')
);
},
+ 'Galerie\Model\GalerieInfoTable' => function($sm) {
+ return new GalerieInfoTable(
+ $sm->get('Zend\Db\Adapter\Adapter')
+ );
+ },
),
);
}
private $_galerieCTable;
private $_galerieCTable2;
private $_galerieCTable3;
+ private $_galerieInfoTable;
private function _getGalerieArrayTable()
return $this->_galerieCTable3;
}
+ private function _getGalerieInfoTable()
+ {
+ if (!$this->_galerieInfoTable) {
+ $sm = $this->getServiceLocator();
+ $this->_galerieInfoTable = $sm->get('Galerie\Model\GalerieInfoTable');
+ }
+ return $this->_galerieInfoTable;
+ }
+
public function indexAction()
{
'GalerieC2_one' => $this->_getGalerieCTable2()->getGalerie(1),
'GalerieC3_all' => $this->_getGalerieCTable3()->all(),
'GalerieC3_one' => $this->_getGalerieCTable3()->get(1),
+ 'GalerieInfo_all' => $this->_getGalerieInfoTable()->select(),
+ 'GalerieInfo_one' => $this->_getGalerieInfoTable()->select(array('gallery.id' => 1))->current(),
+ 'GalerieInfo_usr' => $this->_getGalerieInfoTable()->select(array('gallery.id_user' => 1)),
));
}
));
}
+
+ // Fonctionnalités supplémentaires
+
+ public function get_by_owner($id_user)
+ {
+ return $this->select(array(
+ 'id_user' => (int) $id_user,
+ ));
+ }
+
+ public function delete_by_owner($id_user)
+ {
+ $this->delete(array(
+ 'id_user' => (int) $id_user,
+ ));
+ }
+
+ public function get_by_name($name)
+ {
+ // la colonne 'name' est UNIQUE
+ return $this->one(array(
+ 'name' => name,
+ ));
+ }
+
+ public function find_by_name($name)
+ {
+ // la colonne 'name' est UNIQUE
+ return $this->any(array(
+ 'name' => name,
+ ));
+ }
+
}
--- /dev/null
+<?php
+
+namespace Galerie\Model;
+
+use Custom\Model\Entity;
+
+class GalerieInfo extends Entity
+{
+ public $name;
+ public $description;
+ public $username;
+ public $nb;
+
+
+ protected $columns = array(
+ 'name',
+ 'description',
+ 'username',
+ 'nb',
+ );
+
+ protected $updatable_columns = array(
+ );
+
+ protected $primary_columns = array(
+ );
+
+}
--- /dev/null
+<?php
+namespace Galerie\Model;
+
+use Zend\Db\Adapter\Adapter;
+use Zend\Db\ResultSet\ResultSet;
+use Zend\Db\TableGateway\TableGatewayInterface;
+use Zend\Db\Sql\Sql;
+
+use Custom\Model\Entity;
+
+class GalerieInfoTable implements TableGatewayInterface
+{
+
+ protected $adapter;
+ protected $resultSetPrototype;
+ protected $sql;
+
+ public function __construct(Adapter $adapter) {
+ // Gestion de l'adaptateur
+ if (!$adapter instanceof Adapter) {
+ throw new Exception\RuntimeException('GalerieInfoTable does not have an valid Adapter parameter');
+ }
+ $this->adapter = $adapter;
+
+ // Utilisation du patron de conception Prototype
+ // pour la création des objets ResultSet
+ $this->resultSetPrototype = new ResultSet();
+ $this->resultSetPrototype->setArrayObjectPrototype(
+ new GalerieInfo()
+ );
+
+ // Initialisation de l'outil de création de requête
+ $this->sql = new Sql($this->adapter, $this->getTable());
+ }
+
+
+ public function getTable()
+ {
+ return 'gallery'; // Table centrale de la requête
+ }
+
+
+ public function select($where = null)
+ {
+ $select = $this->sql->select()
+ ->columns(array('name', 'description'))
+ ->join('user', 'gallery.id_user = user.id', array(
+ 'username' => new \Zend\Db\Sql\Expression("user.firstname || ' ' || user.lastname")
+ ))
+ ->join('photo', 'gallery.id = photo.id_gallery', array(
+ 'nb' => new \Zend\Db\Sql\Expression('count(photo.id)')
+ ))
+ ->group(array(
+ 'user.lastname',
+ 'user.firstname',
+ 'gallery.name'
+ ))
+ ->order(array(
+ 'user.lastname',
+ 'user.firstname',
+ 'gallery.name'
+ ));
+ if ($where) {
+ $select->where($where);
+ }
+
+ // prepare and execute
+ $statement = $this->sql->prepareStatementForSqlObject($select);
+ $result = $statement->execute();
+
+ // build result set
+ $resultSet = clone $this->resultSetPrototype;
+ $resultSet->initialize($result);
+
+ return $resultSet;
+ }
+
+ public function insert($set) {
+ throw new \Exception('insert is not allowed');
+ }
+
+ public function update($set, $where = null) {
+ throw new \Exception('update is not allowed');
+ }
+
+ public function delete($where) {
+ throw new \Exception('delete is not allowed');
+ }
+
+}
<p>Identifiant : <?php echo $GalerieC3_one->id; ?></p>
+<h1>GalerieInfo</h1>
+<h2>test de select()</h2>
+
+<pre><?php echo $GalerieInfo_all->count(); ?></pre>
+<pre><?php print_r($GalerieInfo_all->toArray()); ?></pre>
+
+<pre><?php print_r($GalerieInfo_one); ?></pre>
+
+<pre><?php echo $GalerieInfo_usr->count(); ?></pre>
+<pre><?php print_r($GalerieInfo_usr->toArray()); ?></pre>
+
+