What is the most suitable way to work with databases in Zend Framework?

178 views Asked by At

I'm developing web application with Zend Framework. In the documentation it says ...

"Be aware though that the Table Data Gateway pattern can become limiting in larger systems."

  • What are the drawbacks of using Table Data Gateway ?
  • What is the most suitable way to handle database with Zend ?
2

There are 2 answers

2
DevZer0 On BEST ANSWER

Drawbacks of using Table Data Gateway is that it can limit you from writing complex queries. Custom queries such as UNION, SUB QUERY and also using WHERE clauses such as

 WHERE A OR (B AND C) OR D

Best way to write complex queries is to write the complex query by hand. So the best way to execute them is using the Zend_Db_Adapter

0
user2709153 On

In ZF2 there was such thing like Data Mapper (you can see this answer: What is the difference between the Data Mapper, Table Data Gateway (Gateway), Data Access Object (DAO) and Repository patterns?

But if You are using ZF2 this problem has bees solved and the most efficient way to communicate with database is TableGateway with defined ResultSetPrototype (as one of models with exchangeArray function) and ofc trough Zend/Db/Adapter class here is sample how i do it in ZF2:

IN SERVICE.CONFIG:

'Gallery\Model\AnythingListTable' =>  function($sm) {
        $tableGateway = $sm->get('AnythingListTableGateway');
        $table = new AnythingListTable($tableGateway);
        return $table;
    },
    'AnythingListTableGateway' => function ($sm) {
        $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
        $resultSetPrototype = new ResultSet();
        $resultSetPrototype->setArrayObjectPrototype(new Anything());
        return new TableGateway('Anything', $dbAdapter, null, $resultSetPrototype);
    },

AnythingTable is one model (with functions to manipulate database) Anything is second model with filters and rules about data form DB