Looking through several tutorials and books regarding data access in Zend Framework, it seems as if most people do data access within their models (Active Record Pattern) or even controllers. I strongly disagree with that. Therefore I want to have a Data Access Layer (DAL) so that my domain layer remains portable by not having any “ZF stuff” in it. I have searched around but have not really found exactly what I wanted. Heads up: I am new to ZF.
DAL structure
So, the first problem is where to place the Data Access Layer. While it could certainly be placed within the library folder and adding a namespace to the autoloader, that does not seem logical as it is specific to my application (so the applications folder is suitable). I am using a modular structure. I am thinking of using the below structure:
/application/modules/default/dal/
However, I am not sure how include this folder so that I can access the classes within the controllers (without using includes/requires). If anyone knows how to accomplish this, that would be super! Any other ideas are of course also welcome.
The idea is to have my controllers interact with the Data Access Objects (DAO). Then the DAOs use models that can then be returned to the controllers. By doing this, I can leave my models intact.
Implementation
In other languages, I have previously implemented DAOs per model, e.g. DAL_User. This resulted in an awful lot of DAO classes. Is there a smarter way to do this (using a single class does not seem easy with foreign keys)?
I would also appreciate suggestions on how to implement my DAO classes in ZF. I have not spent an awful lot of time reading about all of the components available for database interaction, so any ideas are very welcome. I suspect that there is something smarter than standard PDO available (which probably uses PDO internally, though). Name drops would be sufficient.
Sorry for the many questions. I just need a push in the right direction.
Well, the first thing you have to take into account when dealing with the
Data Access Layer, is that this layer also have sub-layers, it’s unusual to find folders called “dal” in modern frameworks (I’m taking as basis both Zend Framework and Symfony).Second, about
ActiveRecord, you must be aware that by default Zend Frameworks doesn’t implement it. Most of the tutorials take the easiest path to teach new concepts. With simple examples, the amount of business logic is minimal, so instead of adding another layer of complexity (mapping between database and model’s objects) they compose thedomain layer(model) with two basic patterns: Table Data Gateway and Row Data Gateway. Which is enough information for a beginner to start.Additionally, following the Zend Framework Quick Start, on the domain model section, you will realize that there’s a third component, which uses the Data Mapper Pattern.
So, if the main purpose of your
DALis to map data between business objects (model) and your storage, the responsibility of this task is delegated to the Data Mappers as follows:Those methods will interact with the
Database Abstraction Layerand populate the domain objects with the data. Something along this lines:As you can see, the
Data Mappersinteracts with a Zend_Db_Table instance, which uses the Table Data Gateway Pattern. On the other hand, the$this->getDbTable->find()returns instances of the Zend_Db_Table_Row, which implements the Row Data Gateway Pattern (it’s an object representing a database row).The mapping stuff begins here:
So far, I think I have answered your main question, your structure will be as following:
So, as in the ZF Quick Start:
Maybe you want to have a separated folder for the data mappers. Just change:
to
The class name will be
I’ve seen that you want to separate your
domain model objectsinto modules. It’s possible too, all you need is to follow the ZF’s directory and namespace guidelines for modules.That’s it. You still can use your
/daldirectory for storing the DataMappers, just register the namespace, so that the auto loader can find it.