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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:00:48+00:00 2026-05-25T22:00:48+00:00

If I have a fairly complex User model that I would like to use

  • 0

If I have a fairly complex User model that I would like to use the Data Mapping pattern to load, how would I lazily load some of the more intensive bits of user info without allowing the User to be aware of the UserMapper?

For example – if the User model allows for an array of Address objects (and the User might have many of them, but not necessarily needed up front), how would I load those object if/when needed?

Do I make the User model aware of the AddressMapper?

Do I pass the User model BACK into the UserMapper which then hydrates only the Addresses?

Is there a better option?

  • 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-25T22:00:49+00:00Added an answer on May 25, 2026 at 10:00 pm

    Well, I have found the following clever pattern at one time, courtesy of Ben Scholzen, developer for the Zend Framework. It goes something like this:

    class ModelRelation
        implements IteratorAggregate
    {
        protected $_iterator;
        protected $_mapper;
        protected $_method;
        protected $_arguments;
    
        public function __construct( MapperAbstract $mapper, $method, array $arguments = array() )
        {
            $this->_mapper    = $mapper;
            $this->_method    = $method;
            $this->_arguments = $arguments;
        }
    
        public function getIterator()
        {
            if( $this->_iterator === null )
            {
                $this->_iterator = call_user_func_array( array( $this->_mapper, $this->_method ), $this->_arguments );
            }
    
            return $this->_iterator;
        }
    
        public function __call( $name, array $arguments )
        {        
            return call_user_func_array( array( $this->getIterator(), $name ), $arguments );
        }
    }
    

    Ben Scholzen’s actual implementation is here.

    The way you would use it, is something like this:

    class UserMapper
        extends MapperAbstract
    {
        protected $_addressMapper;
    
        public function __construct( AddressMapper $addressMapper )
        {
            $this->_addressMapper = $addressMapper;
        }
    
        public function getUserById( $id )
        {
            $userData = $this->getUserDataSomehow();
    
            $user = new User( $userData );
            $user->addresses = new ModelRelation(
                $this->_addressesMapper,
                'getAddressesByUserId',
                array( $id )
            );
    
            return $user;
        }
    }
    
    class AddressMapper
        extends MapperAbstract
    {
        public function getAddressesByUserId( $id )
        {
            $addressData = $this->getAddressDataSomehow();
    
            $addresses = new SomeAddressIterator( $addressData );
    
            return $addresses;
        }
    }
    
    $user = $userMapper->getUserById( 3 );
    foreach( $user->addresses as $address ) // calls getIterator() of ModelRelation
    {
        // whatever
    }
    

    The thing is though; this could get very slow, if the object graphs get very complex and deeply nested at some point, because the mappers all have to query their own data (presuming you are using a database for persistence). I experienced this when I used this pattern for a CMS to get nested Pages objects (arbitrarily deep child Pages).

    It could probably be tweaked with some caching mechanism, to speed things up considerably though.

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

Sidebar

Related Questions

I have a fat GUI that it getting fairly complex, and I would like
I have a web page that I use to update a fairly complex data
I have a fairly complex data model with approximately 10 entities. Some need to
I have a fairly complex ASP MVC set of controls that set a data
For my app, I have a fairly complex set of configuration options that user
I have a fairly complex method in my controller that basically outputs data to
I have a fairly complex user drawn control that is double buffered. Works very
I have a fairly complex business application written in ASP.NET that is deployed on
I'm writing some fairly complex C# code. I'm finding that my code is throwing
I have a fairly complex web app that was built (by a contractor) to

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.