Database module for Zend Framework 2

August 23rd, 2012 by Cosmin Harangus Leave a reply »

In a previous post I talked about how we can improve the database layer from Zend Framework by extending the Zend_Db_Table_Abstract class and adding various magic functions.

Today I want to talk about how we can do something similar in Zend Framework 2.

The brand new ZF2 database layer allows you to write complex queries, select your own entity classes, quote variables for a specific platform and much much more.

The only downside is that in order to fetch something using the current implementation you need to write something similar to this:

$artistTable = new TableGateway('artist', $adapter);
$rowset = $artistTable->select(array('id' => 2));
$artistRow = $rowset->current();

Although you can certainly get used to writing your own methods and do this in each of them you would loose a lot of time creating the full implementation of your DBAL.

If you’ve used the proposed Ze_Model class or want to try a simpler approach you could check out the ZeDb Module.

Like Ze_Model the ZeDb module allows you to call a variety of magic functions for your Model classes, each executing specific queries on the database and returning your custom Entity classes.

ZeDb allows you to focus on the business side of your application by saving you time when working with the database. It provides you with a powerful set of magic functions that allow you to execute queries easily without the need to write sql queries.

ZeDb works out of the box with entity classes, an even though it provides a base Entity class, you also have the posibility to write your own custom Entities by implementing ZeDb\EntityInterface.

ZeDb works in a similar way with Doctrine, in the fact that it uses a Manager to retrieve model instances. These model instances contain magic functions for accessing the database in order to retrieve Entity objects, remove data or count records.

The Model class defined the following functions:

  • save(EntityInterface $entity): saves an entity record into the database and returns it’s id
  • persist($entities): stores one or more entities locally before saving them into the database
  • flush(): saves all persisted objects into the database
  • get($id): returns an Entity class based on the id of the record

Apart from the above function the model also defines a set of magic functions that can handle combination of table columns, order by or limit.

The pattern used by these methods is as follows:

1. Function name prefix, which can be one of the following:

  • removeBy: removes one or more records from the table
  • getAll: returns all the records from the table
  • getBy: returns a single Entity class based on the values in the specified fields
  • getAllBy: same as getBy only it returns more that one record, if found
  • getLike: returns a single Entity class based on the values in the specified fields using LIKE instead of =
  • getAllLike: same as getLike only it returns more that one record, if found
  • getByColumns: allows you to specify an array of keys and values that can be passed over to the where method of the Select instance before returning a single entity.
  • getAllByColumns: same as getByColumns only it returns more that one record

2. A list of field names in camelCase separated by the “And” word. Ex: $model->getByUsernameAndStatus('paul', 'active');. This is only needed for the following functions: removeBy, getBy, getAllBy, getLike, getAllLike.
3. The “OrderBy” text followed by a list of field names in camelCase separated by the “And” word where each field name can be suffixed by either “Asc” or “Desc”. This section is optional for all functions.
4. The “Limit” text followed by a number representing the maximum number of records that should be returned. If limit is defined then you may also specify an offest from which to start by adding the text “From” followed by the offset number.

The Entity class defined two methods that can help you work with the database faster:

  • save(): saves the current entity instance in the database
  • delete(): removes the entity from the database

Currently the fields for each Entity instance are kept in a data array for fast access and conversion between object and array, but you can always implement the EntityInterface and create your custom Entity classes.

A number of helper functions and optimizations are also scheduled to be added in future versions of the module.

In the meanwhile why not give it a try and see how productive you can be using this approach.

Feedback is always welcome to improve the quality of the next releases.

For any other details on how to install and use the module refer to the module documentation located on github.



  1. Matt says:


    could you post some examples for one-to-many and many-to-many relations?

  2. Cosmin says:

    Hi Matt,

    Currently the module only works with a single table. In order to get records based on one-to-many or many-to-many relations you will need to extend the entity module class with the additional functionality.


  3. Tst says:

    I want to fill a dropdownlist in a module.The dropdownlist is having a data from other module.How can I do it?Please help.

  4. nerijus says:

    Do you have wiki? when I trying your example “$manager = $this->getServiceLocator()->get(‘ZeDbManager’);

    $model = $manager->get(‘Users\Entity\User’);

    $user = $model->getByName(1);

    I get error that model do not have this method. What I do wrong?

Leave a Reply