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 7522061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T02:24:50+00:00 2026-05-30T02:24:50+00:00

I am building an application in Zend Framework. My model is made up of

  • 0

I am building an application in Zend Framework. My model is made up of three layers, domain classes, data mapper classes and a service layer which provides an interface for all external communication. Currently the data mapper classes are only used within my service layer and the domain classes are simple php objects only containing information specific to their domain. A very rough example of what I mean….

//domain class
public class User{
   // data and functions specific to the user domain
}

//service class
public class UserService{
    // service contains an instance of the mapper class
    protected $_mapper = 

    public function fetch($id){
        return $this->_mapper->find($id);
    }
} 

//mapper class
public class UserMapper{
    protected $_dbTable = new Zend_Db_Table('user');

    public function find(){
        return new User($this->_dbTable->find($id)->current());
    }

}

I would retrieve a user object in the controller like so

$user = $this->_service->fetch($id);

This has worked fine so far but now I want to do this

$user = $this->_service->fetch($id);
$recipeCount = $user->getRecipeCount();

The new data comes from another table and I don’t want to pull all this information from the database every time I load a user, I want to lazy load that information when the getRecipeCount function is called. I guess my question is what would be best practice to achieve this? As far as the consumer is concerned the information is specific to the domain of the user so I think that it should be called from the User class. To achieve this the domain class would need to contain its own mapper instance but is that negating the point of having the service class in the first place? I dont really want to be doing this everytime.

$recipeCount = $this->_service->getRecipeCount($user);

Any thoughts will be appreciated

  • 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-05-30T02:24:51+00:00Added an answer on May 30, 2026 at 2:24 am

    Given a User object, there is no doubt tons of stuff that you could ask about this user: get all his groups, get all his posts, get all his recipes, etc.

    It’s tempting to think that each of these must represent a method of the User object itself:

    $groups = $user->getGroups();
    $posts = $user->getPosts();
    $recipes = $user->getRecipes();
    // etc
    

    In this case, an ORM like Doctrine (#FTW!) can help to manage these types of relationships, provide you with a query language with which you can greedy load or lazy load the user entity graph.

    But I think that there is space for a different view here. Why do these have to be methods on the User object itself? Could they instead be methods on service/repository classes:

    $groups  = $groupService->getGroupsByUser($user);
    $posts   = $postService->getPostsbyUser($user);
    $recipes = $recipeService->getRecipesByUser($user);
    // etc
    

    Each of these service/repository classes would probably be instantiated with its own mapper classes for accessing their underlying data sources. This way there is no issue of lazy/greedy on creating/loading the User object. If you want to get the related entities, then go ahead access them via the appropriate service/repository, precisely when you want them.

    Not saying it should always be one way or the other. In fact, saying precisely the opposite: that both approaches have their place. Choosing which one to use in which context involves both some objective analysis of performance and probably no small amount of personal aesthetics.

    Just thinking out loud. YMMV.

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

Sidebar

Related Questions

I am building a Zend Framework application that performs jobs submitted by the user.
I'm building a web application using PHP5.3 and Zend Framework 1.9.4. i have an
I'm building a Zend Framework 1.11.11 application and would like to make the routes
I am building an application based on the Zend Framework, and my issue is
Long story short: I'm building a skeleton application for Zend Framework and I got
I'm building a web application with the Zend Framework. I have wanted to include
I'm building a multi-site Zend Framework application using modules, sort of in the following
I'm building an application in the Zend Framework, but I'd like to implement a
I am building a web application with Zend Framework, and I need to point
I'm currently building a web application (utilizing Zend Framework) and have gotten some of

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.