0

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.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Furquan Ul Haque
  • 15
  • 1
  • 1
  • 7

1 Answers1

0

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);
             },
         ),
     );
    
venca
  • 1,196
  • 5
  • 18
  • Can you please help me on the 3rd step. I already have the factory in module.php as done by the manual. Now that I want to involve another table into it, should I add the new settings in new factories? or add them in the existing one. – Furquan Ul Haque Jun 10 '15 at 10:13
  • If you really have two minutes can you please do the following: edit the getServiceConfig() function for 2 tables. One is already given in the manual, you know for the album table, all I request you to do is add, in the same function the lines for a table named artist. I am only confused because the Module name and the table name are same. So hard to know what that function is doing. – Furquan Ul Haque Jun 10 '15 at 10:16
  • Thats right here http://framework.zend.com/manual/current/en/user-guide/database-and-models.html#using-servicemanager-to-configure-the-table-gateway-and-inject-into-the-albumtable – venca Jun 10 '15 at 10:20
  • This function has settings for Table = Album. I want to know how you will add the setting in this very same function for another Table = Artist. That's all my confusion. – Furquan Ul Haque Jun 10 '15 at 10:24
  • Where did you put the table settings for album ?? Anways I came up with solution. Check my code. Is that what you were trying to tell me? – Furquan Ul Haque Jun 10 '15 at 10:45
  • I suggested you will add your album factories yourself :) – venca Jun 10 '15 at 10:46
  • Thanks for all the help :) – Furquan Ul Haque Jun 10 '15 at 10:48
  • Anyway best practice is to inject dependecies to your controller via factory and not to pull them from service locator in controller directly. http://www.zfdaily.com/2012/07/getting-dependencies-into-zf2-controllers/ – venca Jun 10 '15 at 10:49
  • Thanks for the suggestion. I'll definitely go through that. – Furquan Ul Haque Jun 10 '15 at 10:50