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

  • Home
  • SEARCH
  • 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 8734929
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:56:29+00:00 2026-06-13T09:56:29+00:00

I am starting an application built on zend framework; this application should be able

  • 0

I am starting an application built on zend framework; this application should be able to make use of multiple data sources other than a database; a webservice for example.

I have been reading on how to structure my model so as to allow for this scenario. I have come across various concepts that seem to provide a solution to this (DataMapper Pattern, Service Pattern, Adapter Layer, etc). However, I am still confused on how to put this all together into a reusable and scalable codebase.

I have worked with zend framework before and will normally work with a mysql table. If for example, I have a Users table…I simple have a Users class in my model that contains business logic of the the user domain and a User class extending Zend_Db_Table_Row_Abstract representing a row in the user table.

How best do I organize my model and code base such that i can still call $users->fetchAll() and get a collection of user objects regardless of what my datasource is?

  • 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-13T09:56:30+00:00Added an answer on June 13, 2026 at 9:56 am

    It basically works the same as you did before, just that instead of a Zend_Db_Table_Gateway you use a My_UserGateway_Whatever, e.g. create an interface first:

    interface UserGateway
    {
        /**
         * @return array
         * @throws UserGatewayException
         */
        public function findAll();
    }
    

    We dont want Exceptions from the concrete Gateways to appear in the consuming code, so we add the UserGatewayException as a catch all:

     class UserGatewayException extends RuntimeException
     {}
    

    Then add a class implementing that interface:

    class My_UserGateway_Webservice implements UserGateway
    {
        public function findAll()
        {
            try {
                // insert logic to fetch from the webservice
                return $userData;
            } catch (Exception $e) {
                throw new UserGatewayException($e->getMessage(), $e->getCode, $e);
            }
        }
    
        // … more code
    }
    

    Likewise, if you want to use a Database source, you can write an adapter for the Zend_Db_* classes, e.g.

    class My_UserGateway_Database implements UserGateway
    {
        private $tableDataGateway;
    
        public function __construct(Zend_Db_Table_Abstract $tableDataGateway)
        {
            $this->tableDataGateway = $tableDataGateway;
        }
    
        public function findAll()
        {
            try {
                return $this->tableDataGateway->select()->blah();
            } catch (Exception $e) {
                throw new UserGatewayException($e->getMessage(), $e->getCode, $e);
            }
        }
    
        // … more code
    }
    

    If you need another Data Provider, make sure they implement that interface, so you can rely on the findAll method to be there. Make your consuming class depend on the interface, e.g.

    class SomethingUsingUsers
    {
        private $userGateway;
    
        public function __construct(UserGateway $userGateway)
        {
            $this->userGateway = $userGateway;
        }
    
        public function something()
        {
            try {
                $users = $this->userGateway->findAll();
                // do something with array of user data from gateway
            } catch (UserGatewayException $e) {
                // handle Exception
            }
        }
    
        // … more code
    }
    

    Now, when you create SomethingUsingUsers you can easily inject one or the other Gateway into the constructor and your code will work regardless of which Gateway you used:

    $foo = SomethingUsingUsers(
        new My_UserGateway_Database(
            new Zend_Db_Table('User')
        )
    )
    

    or, for the Webservice:

    $foo = SomethingUsingUsers(
        new My_UserGateway_Webservice(
            // any additional dependencies
        )
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am getting this error when starting JBoss with my application built with warbler:
Is it possible to start service without starting application? I need to refresh data
How can i make something similar like GMail has http://o7.no/H72gPf When application starting, gmail
I'm starting a basic application using Python and PyQt and could use some experienced
I'm using zend mail extended with zend_mail_storage_imap and I built an application that looks
Possible Duplicate: starting an Adobe AIR application multiple times I'm trying to test my
I've built an Android application in eclipse. Before starting the project I selected the
This is seriously starting to bake my noodle. I've just built and deployed (via
I'm starting to write a small application for Linux under the Mono framework, the
I have a Windows Forms application built using the .NET 3.5 Framework which self

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.