zf2.pngI 've been following zf2 from the very first beta versions mainly reading presentations, tutorials and occasionally i browsed the code especially the skeleton application to see how the project progressed. So far i wasn't very impressed with it, the common view as i understand it is that it's not quite there yet and that it needs to be refined more before people really start to jump in and port their old applications to the new framework.

Today thought i started porting my BA thesis application from ZF1 to ZF2 as i plan to extend it  and add new features. The application consists of 5 controllers, a few plugins/helpers and the main engine that produces automatic summarization for Greek language which is a bunch of filters also written in ZF1. Beside that there are Users and Administration systems in place to easily manage the internal data.

I actually managed to accomplice more than i thought for day one. I got the front-end controllers and the summarization engine working with ZF2 quite easily. But still some things really bothered me. The most annoying thing of the day is the new Zend\Db it's obvious that they tried to enforce the Model part of the new MVC concept and it's not there not by a mile not when you have to inject every custom model with the TableGetAway which has to also be injected with the Db\Adapter.  

class Module
    // getAutoloaderConfig() and getConfig() methods here

    // Add this method:
    public function getServiceConfig()
        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);

I am pretty sure there is a smarter way to go that people already use in their modules but that official example is a joke. In my case there are about 4 mysql tables and in the current code base so i didn't bother with models since the system is quite predictable about future features and queries so i prefered the direct approach, here is an example of ZF1 vs ZF2 for running raw queries.

 Zend Framework 1.11

$db = Zend_Db_Table::getDefaultAdapter();
$docu = $db->fetchOne("SELECT COUNT(*) as unprocessed FROM document WHERE processed = 0");

Zend Framework 2.0.3

$db = $this->getServiceLocator()->get('db');
list($docu) = $db->query("SELECT COUNT(*) as total FROM document WHERE processed = 0")->execute()->current();

So my first day wasn't very positive. It was easy to port basic stuff and start working but on the other hand i am not seeing many advantages so far. Keep in touch as i will post daily about my experience with zf2.

Leave a Comment
It will not be published.