title: fetching data from various database tables
body: <p>I'm using zend framework 2.2 on xampp. I've a module 'Album'.</p>
  
  <p><strong>directory structure :</strong></p>
  
  <p><img src="https://i.stack.imgur.com/gqKE8.png" alt="enter image description here"></p>
  
  <p>my database table:'album'</p>
  
  <p><strong>module code :</strong></p>
  
  <pre><code>&lt;?php
  
  namespace Album;
  
  use Album\Model\Album;
  use Album\Model\AlbumTable;
  use Zend\Db\ResultSet\ResultSet;
  use Zend\Db\TableGateway\TableGateway;
  
  class Module {
  
      public function getAutoloaderConfig() {
          return array(
              'Zend\Loader\ClassMapAutoloader' =&gt; array(
                  __DIR__ . '/autoload_classmap.php',
              ),
              'Zend\Loader\StandardAutoloader' =&gt; array(
                  'namespaces' =&gt; array(
                      __NAMESPACE__ =&gt; __DIR__ . '/src/' . __NAMESPACE__,
                  ),
              ),
          );
      }
  
      public function getConfig() {
          return include __DIR__ . '/config/module.config.php';
      }
  
      public function getServiceConfig() {
          return array(
              'factories' =&gt; array(
                  'Album\Model\AlbumTable' =&gt; function($sm) {
                      $tableGateway = $sm-&gt;get('AlbumTableGateway');
                      $table = new AlbumTable($tableGateway);
                      return $table;
                  },
                  'AlbumTableGateway' =&gt; function ($sm) {
                      $dbAdapter = $sm-&gt;get('Zend\Db\Adapter\Adapter');
                      $resultSetPrototype = new ResultSet();
                      $resultSetPrototype-&gt;setArrayObjectPrototype(new Album());
                      return new TableGateway('album', $dbAdapter, null, $resultSetPrototype);
                  },
              ),
          );
      }
  
  }
  </code></pre>
  
  <p><strong>controller code :</strong></p>
  
  <pre><code>&lt;?php
  
  namespace Album\Controller;
  
  use Zend\Mvc\Controller\AbstractActionController;
  use Zend\View\Model\ViewModel;
  use Album\Model\Album;
  use Album\Form\AlbumForm;
  
  class AlbumController extends AbstractActionController {
  
      protected $albumTable;
  
      public function getAlbumTable() {
          if (!$this-&gt;albumTable) {
              $sm = $this-&gt;getServiceLocator();
              $this-&gt;albumTable = $sm-&gt;get('Album\Model\AlbumTable');
          }
          return $this-&gt;albumTable;
      }
  
      public function indexAction() {
          return new ViewModel(array(
                      'albums' =&gt; $this-&gt;getAlbumTable()-&gt;fetchAll(),
                      'active' =&gt; 'albumindex',
                  ));
  }
  }
  </code></pre>
  
  <p><strong>model code :</strong></p>
  
  <pre><code>&lt;?php
  
  namespace Album\Model;
  
  use Zend\Db\ResultSet\ResultSet;
  use Zend\Db\TableGateway\TableGateway;
  use Zend\Db\Sql\Select;
  use Zend\Paginator\Adapter\DbSelect;
  use Zend\Paginator\Paginator;
  
   class AlbumTable
   {
       protected $tableGateway;
  
       public function __construct(TableGateway $tableGateway)
       {
         $this-&gt;tableGateway = $tableGateway;
       }
  
       public function fetchAll($paginated=false)
       {
             $resultSet =$this-&gt;tableGateway-&gt;select(function (Select $select) {
            $select-&gt;order('title ASC');
            });
              return $resultSet;
      }
  }
  </code></pre>
  
  <p><strong>view code :</strong></p>
  
  <pre><code> &lt;?php
   // module/Album/view/album/album/index.phtml:
  
   $title = 'My albums';
   $this-&gt;headTitle($title);
   ?&gt;
   &lt;h1&gt;&lt;?php echo $this-&gt;escapeHtml($title); ?&gt;&lt;/h1&gt;
   &lt;p&gt;
       &lt;a href="&lt;?php echo $this-&gt;url('album', array('action'=&gt;'add'));?&gt;"&gt;Add new album&lt;/a&gt;
   &lt;/p&gt;
  
   &lt;table class="table"&gt;
   &lt;tr&gt;
       &lt;th&gt;Title&lt;/th&gt;
       &lt;th&gt;Artist&lt;/th&gt;
       &lt;th&gt;&amp;nbsp;&lt;/th&gt;
   &lt;/tr&gt;
   &lt;?php foreach ($albums as $album) : ?&gt;
   &lt;tr&gt;
       &lt;td&gt;&lt;?php echo $this-&gt;escapeHtml($album-&gt;title);?&gt;&lt;/td&gt;
       &lt;td&gt;&lt;?php echo $this-&gt;escapeHtml($album-&gt;artist);?&gt;&lt;/td&gt;
       &lt;td&gt;
           &lt;a href="&lt;?php echo $this-&gt;url('album',
               array('action'=&gt;'edit', 'id' =&gt; $album-&gt;id));?&gt;"&gt;Edit&lt;/a&gt;
           &lt;a href="&lt;?php echo $this-&gt;url('album',
               array('action'=&gt;'delete', 'id' =&gt; $album-&gt;id));?&gt;"&gt;Delete&lt;/a&gt;
       &lt;/td&gt;
   &lt;/tr&gt;
   &lt;?php endforeach; ?&gt;
   &lt;/table&gt;
  </code></pre>
  
  <p>I get the output like the following picture.
  <img src="https://i.stack.imgur.com/RkG76.png" alt="enter image description here"></p>
  
  <p>now I want to fetch data from different table('abm') while 'album' table still will be declared in module code.</p>
  
  <pre><code>  return new TableGateway('album', $dbAdapter, null, $resultSetPrototype);
  </code></pre>
  
  <p>(and suppose my both data tables have same structure.so view page and controller will be same.)
  so what should I do for that ? how can I use TableGateway() in model code for different data table ?</p>
  
  <p>-thanks.</p>
  
  <p><strong><em>Edit:</em></strong></p>
  
  <p>I did the following,</p>
  
  <p><strong>Module :</strong></p>
  
  <pre><code>&lt;?php
  
  namespace Album;
  
  use Album\Model\Album;
  use Album\Model\AlbumTable;
  **use Album\Model\AbmTable;**
  use Zend\Db\ResultSet\ResultSet;
  use Zend\Db\TableGateway\TableGateway;
  
  class Module {
  
      public function getAutoloaderConfig() {
          return array(
              'Zend\Loader\ClassMapAutoloader' =&gt; array(
                  __DIR__ . '/autoload_classmap.php',
              ),
              'Zend\Loader\StandardAutoloader' =&gt; array(
                  'namespaces' =&gt; array(
                      __NAMESPACE__ =&gt; __DIR__ . '/src/' . __NAMESPACE__,
                  ),
              ),
          );
      }
  
      public function getConfig() {
          return include __DIR__ . '/config/module.config.php';
      }
  
      public function getServiceConfig() {
          return array(
              'factories' =&gt; array(
                  'Album\Model\AlbumTable' =&gt; function($sm) {
                      $tableGateway = $sm-&gt;get('AlbumTableGateway');
                      $table = new AlbumTable($tableGateway);
                      return $table;
                  },
                  'AlbumTableGateway' =&gt; function ($sm) {
                      $dbAdapter = $sm-&gt;get('Zend\Db\Adapter\Adapter');
                      $resultSetPrototype = new ResultSet();
                      $resultSetPrototype-&gt;setArrayObjectPrototype(new Album());
                      return new TableGateway('album', $dbAdapter, null, $resultSetPrototype);
                  },
                  **'Album\Model\AbmTable' =&gt; function($sm) {
                      $tableGateway = $sm-&gt;get('AbmTableGateway');
                      $table = new AbmTable($tableGateway);
                      return $table;
                  },                        
                  'AbmTableGateway' =&gt; function ($sm) {
                      $dbAdapter = $sm-&gt;get('Zend\Db\Adapter\Adapter');
                      $resultSetPrototype = new ResultSet();
                      $resultSetPrototype-&gt;setArrayObjectPrototype(new Album());
                      return new TableGateway('abm', $dbAdapter, null, $resultSetPrototype);
                  },**                        
              ),
          );
      }
  
  }
  </code></pre>
  
  <p><strong>Controller :</strong></p>
  
  <pre><code>&lt;?php
  
  namespace Album\Controller;
  
  use Zend\Mvc\Controller\AbstractActionController;
  use Zend\View\Model\ViewModel;
  use Album\Model\Album;
  use Album\Form\AlbumForm;
  
  class AlbumController extends AbstractActionController {
  
      protected $albumTable;protected $abmTable;
  
      public function getAlbumTable() {
          if (!$this-&gt;albumTable) {
              $sm = $this-&gt;getServiceLocator();
              $this-&gt;albumTable = $sm-&gt;get('Album\Model\AlbumTable');
          }
          return $this-&gt;albumTable;
      }
  
      **public function getAbmTable() {
          if (!$this-&gt;abmTable) {
              $sm = $this-&gt;getServiceLocator();
              $this-&gt;abmTable = $sm-&gt;get('Album\Model\AbmTable');
          }
          return $this-&gt;abmTable;
      }**    
  
      public function indexAction() {
          return new ViewModel(array(
                      'albums' =&gt; $this-&gt;getAlbumTable()-&gt;fetchAll(),
                      **'abms' =&gt; $this-&gt;getAbmTable()-&gt;fetchAll(),**
                  ));
       }
  }
  </code></pre>
  
  <p><strong>Model (a new model page: 'AbmTable.php') :</strong></p>
  
  <pre><code>&lt;?php
  
  namespace Album\Model;
  
  use Zend\Db\ResultSet\ResultSet;
  use Zend\Db\TableGateway\TableGateway;
  use Zend\Db\Sql\Select;
  use Zend\Paginator\Adapter\DbSelect;
  use Zend\Paginator\Paginator;
  
   class AbmTable
   {
       protected $tableGateway;
  
       public function __construct(TableGateway $tableGateway)
       {
         $this-&gt;tableGateway = $tableGateway;
       }
  
       public function fetchAll($paginated=false)
       {
             $result =$this-&gt;tableGateway-&gt;select(function (Select $select) {
            $select-&gt;order('title ASC');
            });
              return $result;
      }
   }
  </code></pre>
  
  <p>then I received '$abms' in view page with 'abm' table data.</p>
  
  <p><strong>Update :</strong></p>
  
  <p>it fetches different tables with same model.</p>
  
  <p><strong>Module :</strong></p>
  
  <pre><code>namespace Album;
  
  use Album\Model\AlbumTable;
  use Zend\Db\ResultSet\ResultSet;
  use Zend\Db\TableGateway\TableGateway;
  
  class Module {
  
      public function getAutoloaderConfig() {
          return array(
              'Zend\Loader\ClassMapAutoloader' =&gt; array(
                  __DIR__ . '/autoload_classmap.php',
              ),
              'Zend\Loader\StandardAutoloader' =&gt; array(
                  'namespaces' =&gt; array(
                      __NAMESPACE__ =&gt; __DIR__ . '/src/' . __NAMESPACE__,
                  ),
              ),
          );
      }
  
      public function getConfig() {
          return include __DIR__ . '/config/module.config.php';
      }
  
      public function getServiceConfig() {
          return array(
              'factories' =&gt; array(
  // 'album' table-------------------------------------                 
                  'Album\Model\AlbumTable\dbtable=album' =&gt; function($sm) {
                      $tableGateway = $sm-&gt;get('AlbumTableGateway');
                      $table = new AlbumTable($tableGateway);
                      return $table;
                  },
                  'AlbumTableGateway' =&gt; function ($sm) {
                      $dbAdapter = $sm-&gt;get('Zend\Db\Adapter\Adapter');
                      $resultSetPrototype = new ResultSet();
                      $resultSetPrototype-&gt;setArrayObjectPrototype(new Album());
                      return new TableGateway('album', $dbAdapter, null, $resultSetPrototype);
                  },
  // 'abm' table-------------------------------------                        
                  'Album\Model\AlbumTable\dbtable=abm' =&gt; function($sm) {
                      $tableGateway = $sm-&gt;get('AbmTableGateway');
                      $table = new AlbumTable($tableGateway);
                      return $table;
                  },                        
                  'AbmTableGateway' =&gt; function ($sm) {
                      $dbAdapter = $sm-&gt;get('Zend\Db\Adapter\Adapter');
                      $resultSetPrototype = new ResultSet();
                      $resultSetPrototype-&gt;setArrayObjectPrototype(new Album());
                      return new TableGateway('abm', $dbAdapter, null, $resultSetPrototype);
                  },
              ),
          );
      }
  }
  </code></pre>
  
  <p><strong>Controller :</strong></p>
  
  <pre><code>namespace Album\Controller;
  
  use Zend\Mvc\Controller\AbstractActionController;
  use Zend\View\Model\ViewModel;
  
  class AlbumController extends AbstractActionController {
  
      protected $albumTable;
      protected $abmTable;
  
      public function getAlbumTable() {
          if (!$this-&gt;albumTable) {
              $sm = $this-&gt;getServiceLocator();
              $this-&gt;albumTable = $sm-&gt;get('Album\Model\AlbumTable\dbtable=album');
          }
          return $this-&gt;albumTable;
      }
  
      public function getAbmTable() {
          if (!$this-&gt;abmTable) {
              $sm = $this-&gt;getServiceLocator();
              $this-&gt;abmTable = $sm-&gt;get('Album\Model\AlbumTable\dbtable=abm');
          }
          return $this-&gt;abmTable;
      }    
  
      public function indexAction() {
          return new ViewModel(array(
                      'albums' =&gt; $this-&gt;getAlbumTable()-&gt;fetchAll(),
                      'abms' =&gt; $this-&gt;getAbmTable()-&gt;fetchAll(),
                  ));
      }
   }
  </code></pre>
  
  <p><strong>Model :</strong></p>
  
  <pre><code>namespace Album\Model;
  
  use Zend\Db\ResultSet\ResultSet;
  use Zend\Db\TableGateway\TableGateway;
  use Zend\Db\Sql\Select;
  
   class AlbumTable
   {
       protected $tableGateway;
  
       public function __construct(TableGateway $tableGateway)
       {
         $this-&gt;tableGateway = $tableGateway;
       }
  
       public function fetchAll($paginated=false)
       {
             $result =$this-&gt;tableGateway-&gt;select(function (Select $select) {
            $select-&gt;order('title ASC');
            });
              return $result;
      }
   }
  </code></pre>