简体   繁体   中英

Alternative to TableGateway for Zend Framework 2

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.

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.

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 ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM