Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8432371
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T06:06:32+00:00 2026-06-10T06:06:32+00:00

Looking through several tutorials and books regarding data access in Zend Framework, it seems

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T06:06:34+00:00Added an answer on June 10, 2026 at 6:06 am

    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 the domain layer (model) with two basic patterns: Table Data Gateway and Row Data Gateway. Which is enough information for a beginner to start.

    After analyzing it, you will see some similarity between ActiveRecord
    and Row Data Gateway patterns. The main difference is that
    ActiveRecord objects (persistable entities) carries business logic and
    Row Data Gateway only represents a row in the database. If you add
    business logic on a object representing a database row, then it will
    become an ActiveRecord object.

    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 DAL is to map data between business objects (model) and your storage, the responsibility of this task is delegated to the Data Mappers as follows:

    class Application_Model_GuestbookMapper
    {
    
        public function save(Application_Model_Guestbook $guestbook);
    
        public function find($id);
    
        public function fetchAll();
    
    }
    

    Those methods will interact with the Database Abstraction Layer and populate the domain objects with the data. Something along this lines:

    public function find($id, Application_Model_Guestbook $guestbook)
    {
    
        $result = $this->getDbTable()->find($id);
    
        if (0 == count($result)) {
    
            return;
    
        }
    
        $row = $result->current();
    
        $guestbook->setId($row->id)
    
                  ->setEmail($row->email)
    
                  ->setComment($row->comment)
    
                  ->setCreated($row->created);
    
    }
    

    As you can see, the Data Mappers interacts 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).

    Tip: The domain object itself, the guestbook
    entity, was not created by the find() method on the DataMapper,
    instead, the idea is that object creation is a task of factories
    and you must inject the dependency in order to achieve the so called
    Dependency Inversion Principle (DIP) (part of the SOLID principles). But that’s
    another subject, out of the scope of the question. I suggest you
    to access the following link http://youtu.be/RlfLCWKxHJ0

    The mapping stuff begins here:

    $guestbook->setId($row->id)
              ->setEmail($row->email)
              ->setComment($row->comment)
              ->setCreated($row->created);
    

    So far, I think I have answered your main question, your structure will be as following:

    application/models/DbTable/Guestbook.php
    application/models/Guestbook.php
    application/models/GuestbookMapper.php
    

    So, as in the ZF Quick Start:

    class GuestbookController extends Zend_Controller_Action
    {
    
        public function indexAction()
    
        {
            $guestbook = new Application_Model_GuestbookMapper();
    
            $this->view->entries = $guestbook->fetchAll();
    
        }
    
    }
    

    Maybe you want to have a separated folder for the data mappers. Just change:

    application/models/GuestbookMapper.php
    

    to

    application/models/DataMapper/GuestbookMapper.php
    

    The class name will be

    class Application_Model_DataMapper_GuestbookMapper
    

    I’ve seen that you want to separate your domain model objects into modules. It’s possible too, all you need is to follow the ZF’s directory and namespace guidelines for modules.

    Final tip: I’ve spent a lot of time coding my own data mappers for
    finally realize that it’s nightmare to maintain the object mapping when
    your application grows with a lot of correlated entities. (i.e Account
    objects that contain references to users objects, users that contain
    roles, and so on) It’s not so easy to write the mapping stuff at this
    point. So I strongly recommend you, if you really want a true
    object-relational mapper, to first study how legacy frameworks perform
    such tasks and perhaps use it.
    So, take some spare time with Doctrine 2, which is the
    one of the best so far (IMO) using the DataMapper pattern.

    That’s it. You still can use your /dal directory for storing the DataMappers, just register the namespace, so that the auto loader can find it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looking through the Vows documentation and in several places it uses the syntax
Looking through a few SQL implementations, I noticed that most DBMSs support defining an
I've spent a couple of hours looking through several the similar answers before posting
I'm looking through several documents but for some reason I can't wrap my head
I'm cycling through several different attributes looking for something not set to To do
Here's the situation i'm passing a object through several php functions. I m looking
Looking through my VisualSVNServer logs, I'm seeing several attempts from different client IP addresses
I'm working on an application that will gather data through HTTP from several places,
I've read several questions here where people run minimal Qt programs through valgrind, and
I was looking through some of the .net source yesterday and saw several implementations

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.