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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:28:41+00:00 2026-05-13T08:28:41+00:00

I currently work on a small application with models, mappers and controllers. My question

  • 0

I currently work on a small application with models, mappers and controllers.
My question is, (because I did not found any matching answer), how does the mapper interact with the model (& the controller), when we have the following situation.

$user = new UserModel();
$user->setId('21');
$userMapper = new UserMapper($user);
$userMapper->retrieve();

This will work as fine as possible, the model has an id with which the mapper can retrieve the needed user (and map it back into an user object).

My problem is, how can I wrap this code, I mean, this code is very raw and it is definitly not very recommended to be used in a controller.
I want to shorten it, but I do not exactly know how:

public function view($id)
{
     $user->find($id); // this seems always to be tied with the user object/model (e.g. cakephp), but I think the ->find operation is done by the mapper and has absolutly nothing to do with the model
     $view->assign('user',$user);
}

It should look more like:

public function view($id)
{
    $mapper = $registry->getMapper('user');
    $user = $mapper->find($id);
    // or a custom UserMapper method:
    # $user = $mapper->findById($id);
    $view->assign('user',$user);
}

But this is much more code.
Should I include the getMapper procedure within the parent controller class, so I can easily access $this->_mapper without explicitly calling it?

The problem is, I do not want to break the mapper pattern, so the the Model should not access any SQL/Mapper method directly by $model->find(), but I do not want to have a lot of code just for first creating a mapper and do this and this etc.

I hope you’ll understand me a little bit, myself is already confused enough, because I am new to many patterns and mapping/modelling techniques.

  • 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-13T08:28:42+00:00Added an answer on May 13, 2026 at 8:28 am

    You could add a Service Layer, e.g.

    class UserService
    {
        public function findUserById($id)
        {
            // copied and adjusted from question text
            $user = new UserModel();
            $user->setId($id);
            $userMapper = new UserMapper($mapper);
            return $userMapper->retrieve();
        }
    }
    

    Your controller won’t access the UserModel and the UserMapper directly, but through the Service.

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

Sidebar

Related Questions

I'm currently developing a small application in WPF for us at work though I'm
I'm currently working on a small webapp where I work. I've created this for
I am currently building a small web application for publishing stories using PHP and
I have written a small application which will be used in my work environment
We're a small team of developers and building a web application. We're currently having
I'm currently trying to use Google Guice-3.0 in a small application. When running this
I'm currently developing a small WPF application using a file database (SQLCe). Since I'm
I'm currently working on an internal sales application for the company I work for,
Currently in my application and using the unit of work pattern and generic repository,
I currently have a small console application that runs a number of Tasks (using

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.