Jason Grimes: Using Doctrine 2 in Zend Framework 2

30th January 2012

Jason Grimes has posted an article showing how to use Doctrine 2 with Zend Framework 2.

He uses my tutorial as the starting point which enables him to concentrate on the Doctrine integration rather than the irrelevant details about setting a ZF2 application which is excellent.

He walks through 6 steps in order to do the integration:

This article shows how to set up and use Doctrine 2 in Zend Framework 2, by extending Rob’s Getting Started tutorial to use Doctrine instead of Zend_Db.

  • Start with Akrabat’s tutorial
  • Install Doctrine modules
  • Configure the Album module to use Doctrine
  • Create the Album entity
  • Update the Album controller to use Doctrine instead of Zend_Db
  • That’s it!

I highly recommend having a read if you're at all interested in using Doctrine 2 with Zend Framework 2.

Zend Framework 2 Beta 2 released

21st December 2011

Zend Framework 2, Beta 2 has been released!

The key new features are:

  • Refactored Mail component
  • Refactored Cache component
  • MVC updates

Check out Matthew's blog post for the full details.

I've also updated my tutorial. This is a good time to get involved, try it out and let us know what you like/dislike.

Updated tutorial for Zend Framework 2 beta 1

18th October 2011

With the announcement of Zend Framework beta 1, I have updated my venerable tutorial to work with it!

Getting started with Zend Framework 2 (beta1), creates the same application as my ZF1 tutorial, so it should be very familiar, but this time, it's in the context of Zend Framework 2. As usual, it's a PDF too.

Please download it, try it out and let me know if you find any typos!

Zend_Config_Ini and a string

20th June 2011

One thing that is different between Zend_Config_Xml and Zend_Config_Ini is that with Zend_Config_Xml you can pass in an XML string as the first parameter of the constructor and it will work. This doesn't work with Zend_Config_Ini as we use parse_ini_file() under the hood.

With PHP 5.3 however there is is a new function called parse_ini_string() which will allow us to load arbitrary ini string into Zend_Config objects. This can't go into Zend Framework 1 though due to our PHP 5.2.4 minimum version requirement.

As I needed this for a project, I extended Zend_Config_Ini to support this feature, which means simply overloading a single method

class App_Config_Ini extends Zend_Config_Ini
{
    /**
     * Load the INI file from disk using parse_ini_file(). Use a private error
     * handler to convert any loading errors into a Zend_Config_Exception
     *
     * @param string $filename
     * @throws Zend_Config_Exception
     * @return array
     */
    protected function _parseIniFile($filename)
    {
        set_error_handler(array($this'_loadFileErrorHandler'));
        if (substr($filename, -4) == '.ini') {
            $iniArray parse_ini_file($filenametrue);
        } else {        
            $iniArray parse_ini_string($filenametrue);
        }
        restore_error_handler();

        // Check if there was a error while loading file
        if ($this->_loadFileErrorStr !== null) {
            /**
             * @see Zend_Config_Exception
             */
            require_once 'Zend/Config/Exception.php';
            throw new Zend_Config_Exception($this->_loadFileErrorStr);
        }

        return $iniArray;
    }
}

The actual change is to see if the last 4 characters of the filename are ".ini" and if they aren't then use parse_ini_string() instead of parse_ini_file(). The rest of the code is just error handling.

This is one area where I really like it when a class implements methods that done just one thing.

Exploring Zend_Paginator

4th May 2011

One area of displaying lists on web pages that I've generally disliked doing is pagination as it's a bit of a faff. Recently, I needed to do just this though as I couldn't delegate it as my colleague was too busy on other work. As a result, I thought that I should look into Zend_Paginator this time. Turns out that it's really easy to use and the documentation is great too.

The really useful thing about Zend_Paginator is that it uses adapters to collect its data. There are a variety of adapters, including array, dbSelect, dbTableSelect and iterator. The interesting ones for me being dbSelect and dbTableSelect as I use Zend_Db based data access layers.

This is how I used it with a Zend_Db based data mapper within TodoIt.

Setting up the paginator

My current method looks like this:

class Application_Model_TaskMapper
{
    public function fetchOutstanding()
    {
        $db $this->getDbAdapter();
        $select $db->select();
        $select->from($this->_tableName);
        $select->where('date_completed IS NULL');
        $select->order(array('due_date ASC''id DESC'));
        $rows $db->fetchAll($select);
        foreach ($rows as $row) {
            $task = new Application_Model_Task($row);
            $tasks[] = $task;
        }
        return $tasks;
    }

    // etc

This is pretty standard code for a data mapper. We select the data from the database and convert it to an array of entities. For the paginator to do its stuff though, we have to pass it the select object so that it can set the limit() on the select object.

The code therefore becomes:

    public function fetchOutstanding()
    {
        $db $this->getDbAdapter();
        $select $db->select();
        $select->from($this->_tableName);
        $select->where('date_completed IS NULL');
        $select->order(array('date_completed DESC''id DESC'));
        
        $adapter = new Zend_Paginator_Adapter_DbSelect($select);
        $paginator = new Zend_Paginator($adapter);
        return $paginator;
    }

As you can see, we create an instance of Zend_Paginator_Adapter_DbSelect which takes the $select object and the instantiate a Zend_Paginator and return it. The Zend_Paginator object implements Interator, so you can use it exactly like an array in a foreach loop and hence, in theory, your view script doesn't need to change.

However, the code that consumes TaskMapper expects an array of Task objects, not an array of arrays. To tell the paginator to create our objects, we extend Zend_Paginator_Adapter_DbSelect and override getItems() like this:

class Application_Model_Paginator_TaskAdapter extends Zend_Paginator_Adapter_DbSelect
{
    /**
     * Returns an array of items for a page.
     *
     * @param  integer $offset Page offset
     * @param  integer $itemCountPerPage Number of items per page
     * @return array
     */
    public function getItems($offset$itemCountPerPage)
    {
        $rows parent::getItems($offset$itemCountPerPage);
        
        $tasks = array();
        foreach ($rows as $row) {
            $task = new Application_Model_Task($row);
            $tasks[] = $task;
        }
        return $tasks;
    }
}

Here, we've used the entity-creation code that was in our original implementation of fetchOutstanding() and placed it in getItems().

Obviously we have to update fetchOutstanding() to use our new adapter, so we replace

$adapter = new Zend_Paginator_Adapter_DbSelect($select);

with

$adapter = new Application_Model_Paginator_TaskAdapter($select);

Now, when we iterate over the pagination object, we get instances of Task and all is well with the world.

Using the paginator

Now that we have a paginator in place, we need to use it. Specifically we need to tell the paginator which page number we want to view and how many items are on a page. Within TodoIt, this is done in the ServiceLayer object and looks something like this:

class Application_Service_TaskService
{
    // ...

    public function fetchOutstanding($page$numberPerPage 25)
    {
        $mapper = new Application_Model_TaskMapper();
        $tasks $mapper->fetchOutstanding();
        $tasks->setCurrentPageNumber($page);
        $tasks->setItemCountPerPage($numberPerPage);        
        return $tasks;
    }

    // ...

Clearly the $page parameter comes via the URL at some point, so the controller looks something like this:

class IndexController extends Zend_Controller_Action
{
    public function indexAction()
    {
        $page $this->_getParam('page'1);

        $taskService = new Application_Service_TaskService();
        $this->view->outstandingTasks $taskService->fetchOutstanding($page);
        
        $messenger $this->_helper->flashMessenger;
        $this->view->messages $messenger->getMessages();
    }

    //...

and then the view uses a foreach as you'd expect.

Adding the paging controls

Finally, to complete a paged list, we have to provide the user a mechanism to select the next and previous pages along with maybe jumping to a specific page. This is done using a separate view script that you pass to the paginator. In your view script, you put something like:

<?php echo $this->paginationControl($this-> outstandingTasks,
                    'Sliding',
                    'pagination_control.phtml'); ?>

The first parameter is your paginator object. The second is the 'scrolling style' to use. There are four choices documented in the manual: All, Elastic, Jumping and Sliding. Personally, I have chosen to not display the page numbers themselves, so it doesn't matter which one I pick. The last parameter is the partial view script that you want to be rendered. This allows you to have complete customisation of the HTML.

Here's what I'm using which is based heavily on and example in the documentation:

<?php if ($this->pageCount): ?>
<div class="pagination-control">
<!-- Previous page link -->
<?php if (isset($this->previous)): ?>
  <a href="<?php echo $this->url(array('page' => $this->previous)); ?>">
    Previous
  </a> |
<?php else: ?>
  <span class="disabled">&lt; Previous</span> |
<?php endif; ?>

<!-- Next page link -->
<?php if (isset($this->next)): ?>
  <a href="<?php echo $this->url(array('page' => $this->next)); ?>">
    Next &gt;
  </a>
<?php else: ?>
  <span class="disabled">Next &gt;</span>
<?php endif; ?>
<span class="pagecount">
    Page <?php echo $this->current?> of <?php echo $this->pageCount?>
</span>
</div>
<?php endif; ?>

And that's it; I now have paginated tasks in TodoIt and as you can see, Zend_Paginator is very easy to use and, more importantly, simple to customise to your own needs.