Search code examples
phpmysqlmodulezend-framework2tablegateway

Accessing 2 different tables from same controller file


I have successfully completed the the album table tutorial in Zend Framework 2 manual. I have implemented using tablegateway. I can see that he used only one table named "album" and hence he implemented according to that one table.

Lets say I have another table called "artist" that holds the information of each artists from that "album" table.

Now in manual, he simply uses:

$this->getAlbumTable()->fetchAll();

Now I want to do similar thing with "artists" table so that my query can be like:

$this->getArtistsTable()->fetchAll();

So what should I change or add?? I already have created the artist table in mySql with columns: Name, DOB, Country.

P.S: I am not going to use joins or anything atm. Just want to access the second table in same controller (same module).

SOLUTION: With the help of Venca, I was able to solve this problem: here is how you should edit the factory setting for 2 tables and more.

public function getServiceConfig()
{
    // Given in Manual
    return array(
        'factories' => array(
            'Album\Model\AlbumTable' =>  function($sm) {
                $tableGateway = $sm->get('AlbumTableGateway');
                $table = new AlbumTable($tableGateway);
                return $table;
             },
             'AlbumTableGateway' => function ($sm) {
                  $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                  $resultSetPrototype = new ResultSet();
                  $resultSetPrototype->setArrayObjectPrototype(new Album());
                  return new TableGateway('album', $dbAdapter, null, $resultSetPrototype);
              },

          // Added another table named Artist
              'Album\Model\ArtistTable' =>  function($sm) {
                     $tableGateway = $sm->get('ArtistTableGateway');
                     $table = new AlbumTable($tableGateway);
                     return $table;
               },
               'ArtistTableGateway' => function ($sm) {
                     $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                     $resultSetPrototype = new ResultSet();
                     $resultSetPrototype->setArrayObjectPrototype(new Artist());
                     return new TableGateway('artist', $dbAdapter, null, $resultSetPrototype);
                },
             ),
         ); 
     }

Now you can access both tables from your controller. Problem Solved. Day well spent.


Solution

  • According to manual

    1. Create model
    2. Create model table
    3. Add factory to your module

       return array(
           'factories' => array(
               'Album\Model\ArtistTable' =>  function($sm) {
                   $tableGateway = $sm->get('ArtistTableGateway');
                   $table = new ArtistTable($tableGateway);
                   return $table;
               },
               'ArtistTableGateway' => function ($sm) {
                   $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                   $resultSetPrototype = new ResultSet();
                   $resultSetPrototype->setArrayObjectPrototype(new Artist());
                   return new TableGateway('artist', $dbAdapter, null, $resultSetPrototype);
               },
           ),
       );