Alternative to TableGateway for Zend Framework 2

816 views Asked by At

I'm using Zend framework 2 to create web pages.

Through TableGateway I'm accessing my PostgreSQL DB.

Is there any alternative to TableGateway that can be used effectively in Zend framework 2, with easy-to-use documentation and stuff?

I'm trying to get away from Doctrine 2 (unless you could convince me otherwise).

Any help would be appretiated.

2

There are 2 answers

0
Fiambre On BEST ANSWER

As I commented, I used 3 ways to handle data access.

  • TableGateway patter is ok but it's not easy to maintain for larger applications.

  • Another commonly used pattern is the data mapper pattern, for this you can use AbstractDbMapper and hydrator for mapping. AbstracDbMapper Link

  • Entity pattern, like Doctrine

I finally decided to use doctrine basically cause is very easy to maintain and you can save some hours of code, but if you don't want to use doctrine i think that Data Mapper Pattern is the best option.

2
kuldeep.kamboj On

What is really confusing? If you are using Zend MVC, Then Service Manager/Locator is using every where and that is way to define TableGateway/DB Models too. Model's code still good and understanding. I don't understand where is really confusing.

One change I can suggest the original ZF 2.0 style simpler getServiceConfig which is working for me till now.

'Album\Model\AlbumTable' =>  function($sm) {
    $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
    $table = new Model\AlbumTable($dbAdapter);
    return $table;
},

Instead of

'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);
 },

I did not faced any problem by using previous one but can't say if others faced ?