use Galerie\Model\GalerieArrayTable;
use Galerie\Model\GalerieATable;
use Galerie\Model\GalerieBTable;
+use Galerie\Model\GalerieCTable;
class Module implements
AutoloaderProviderInterface,
$sm->get('Zend\Db\Adapter\Adapter')
);
},
+ 'Galerie\Model\GalerieCTable' => function($sm) {
+ return new GalerieCTable(
+ $sm->get('Zend\Db\Adapter\Adapter')
+ );
+ },
),
);
}
private $_galerieArrayTable;
private $_galerieATable;
+ private $_galerieBTable;
+ private $_galerieCTable;
+
private function _getGalerieArrayTable()
{
return $this->_galerieBTable;
}
+ private function _getGalerieCTable()
+ {
+ if (!$this->_galerieCTable) {
+ $sm = $this->getServiceLocator();
+ $this->_galerieCTable = $sm->get('Galerie\Model\GalerieCTable');
+ }
+ return $this->_galerieCTable;
+ }
+
public function indexAction()
{
return new ViewModel(array(
'GalerieA_one' => $this->_getGalerieATable()->getGalerie(1),
'GalerieB_all' => $this->_getGalerieBTable()->fetchAll(),
'GalerieB_one' => $this->_getGalerieBTable()->getGalerie(1),
+ 'GalerieC_all' => $this->_getGalerieCTable()->fetchAll(),
+ 'GalerieC_one' => $this->_getGalerieCTable()->getGalerie(1),
));
}
--- /dev/null
+<?php
+
+namespace Galerie\Model;
+
+class GalerieC
+{
+ public $id;
+ public $id_user;
+ public $name;
+ public $description;
+ public $created;
+ public $updated;
+
+
+ protected $columns = array(
+ 'id',
+ 'id_user',
+ 'name',
+ 'description',
+ 'created',
+ 'updated',
+ );
+
+ protected $updatable_columns = array(
+ 'id_user',
+ 'name',
+ 'description',
+ 'created',
+ 'updated',
+ );
+
+ protected $primary_columns = array(
+ 'id',
+ );
+
+ public function exchangeArray($data, $overwrite=true)
+ {
+ foreach($this->columns as $col) {
+ if (array_key_exists($col, $data)) {
+ $this->$col = $data[$col];
+ } elseif ($overwrite) {
+ $this->$col = null;
+ }
+ }
+ }
+
+ public function toArray() {
+ $result = array();
+ foreach($this->columns as $col) {
+ $result[$col] = $this->$col;
+ }
+ return $result;
+ }
+
+ public function toUpdatableArray() {
+ $result = array();
+ foreach($this->updatable_columns as $col) {
+ $result[$col] = $this->$col;
+ }
+ return $result;
+ }
+
+ public function toPrimaryArray() {
+ $result = array();
+ foreach($this->primary_columns as $col) {
+ $result[$col] = $this->$col;
+ }
+ return $result;
+ }
+
+}
--- /dev/null
+<?php
+namespace Galerie\Model;
+
+use Zend\Db\Adapter\Adapter;
+use Zend\Db\ResultSet\ResultSet;
+use Zend\Db\TableGateway\AbstractTableGateway;
+
+
+class GalerieCTable extends AbstractTableGateway
+{
+ public $table = 'gallery';
+
+ public function __construct(Adapter $adapter)
+ {
+ // Composition avec l'adaptateur
+ $this->adapter = $adapter;
+
+ // Utilisation du patron de conception Prototype
+ // pour la création des objets ResultSet
+ $this->resultSetPrototype = new ResultSet();
+ $this->resultSetPrototype->setArrayObjectPrototype(
+ new GalerieC()
+ );
+
+ // Initialisation du gestionnaire
+ $this->initialize();
+ }
+
+ public function fetchAll()
+ {
+ return $this->select();
+ }
+
+ public function getGalerie($id)
+ {
+ if ($id === null) {
+ $row = null;
+ } else {
+ $row = $this->select(array(
+ 'id' => (int) $id,
+ ))->current();
+ }
+ if (!$row) {
+ throw new \Exception("cannot get row $id in table 'galerie'");
+ }
+ return $row;
+ }
+
+ public function saveGalerie(GalerieC $galerie)
+ {
+ if ($galerie->id === null) {
+ $this->insert(
+ $galerie->toUpdatableArray()
+ );
+ } elseif ($this->getGalerie($galerie->id)) {
+ $this->update(
+ $galerie->toUpdatableArray(),
+ $galerie->toPrimaryArray()
+ );
+ } else {
+ throw new \Exception("cannot update row {$galerie->id} in table 'galerie'");
+ }
+ }
+
+ public function deleteGalerie($id)
+ {
+ $this->delete(array(
+ 'id' => (int) $id
+ )
+ );
+ }
+
+}
<pre><?php print_r($GalerieB_one); ?></pre>
<p>Identifiant : <?php echo $GalerieB_one->getId(); ?></p>
+
+
+<h1>GalerieC</h1>
+<h2>test de fetchAll()</h2>
+
+<pre><?php echo $GalerieC_all->count(); ?></pre>
+<pre><?php print_r($GalerieC_all->toArray()); ?></pre>
+
+<h2>test de getGalerie</h2>
+
+<pre><?php print_r($GalerieC_one); ?></pre>
+<p>Identifiant : <?php echo $GalerieC_one->id; ?></p>