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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:44:38+00:00 2026-05-16T22:44:38+00:00

I’m working with Doctrine2 for the first time, but I think this question is

  • 0

I’m working with Doctrine2 for the first time, but I think this question is generic enough to not be dependent on a specific ORM.

Should the entities in a Data Mapper pattern be aware – and use – the Mapper?

I have a few specific examples, but they all seem to boil down to the same general question.

If I’m dealing with data from an external source – for example a User has many Messages – and the external source simply provides the latest few entities (like an RSS feed), how can $user->addMessage($message) check for duplicates unless it either is aware of the Mapper, or it ‘searches’ through the collection (seems like an inefficient thing to do).

Of course a Controller or Transaction Script could check for duplicates before adding the message to the user – but that doesn’t seem quite right, and would lead to code duplication.

If I have a large collection – again a User with many Messages – how can the User entity provide limiting and pagination for the collection without actually proxying a Mapper call?

Again, the Controller or Transaction Script or whatever is using the Entity could use the Mapper directly to retrieve a collection of the User‘s Messages limited by count, date range, or other factors – but that too would lead to code duplication.

Is the answer using Repositories and making the Entity aware of them? (At least for Doctrine2, and whatever analogous concept is used by other ORMs.) At that point the Entity is still relatively decoupled from the Mapper.

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

    Rule #1: Keep your domain model simple and straightforward.

    First, don’t prematurely optimize something because you think it may be inefficient. Build your domain so that the objects and syntax flow correctly. Keep the interfaces clean: $user->addMessage($message) is clean, precise and unambiguous. Underneath the hood you can utilize any number of patterns/techniques to ensure that integrity is maintained (caching, lookups, etc). You can utilize Services to orchestrate (complex) object dependencies, probably overkill for this but here is a basic sample/idea.

    class User
    {
      public function addMessage(Message $message)
      {
         // One solution, loop through all messages first, throw error if already exists
         $this->messages[] $message;
      }
      public function getMessage()
      {
         return $this->messages;
      }
    }
    class MessageService
    {
      public function addUserMessage(User $user, Message $message)
      {
         // Ensure unique message for user
         // One solution is loop through $user->getMessages() here and make sure unique
         // This is more or less the only path to adding a message, so ensure its integrity here before proceeding 
         // There could also be ACL checks placed here as well
         // You could also create functions that provide checks to determine whether certain criteria are met/unmet before proceeding
         if ($this->doesUserHaveMessage($user,$message)) {
           throw Exception...
         }
         $user->addMessage($message);
      }
      // Note, this may not be the correct place for this function to "live"
      public function doesUserHaveMessage(User $user, Message $message)
      {
         // Do a database lookup here
         return ($user->hasMessage($message) ? true
      }
    }
    class MessageRepository
    {
      public function find(/* criteria */)
      {
         // Use caching here
         return $message;
      }
    }
    
    class MessageFactory
    {
       public function createMessage($data)
       {
         //
         $message = new Message();
         // setters
         return $message;
       }
    }
    
    // Application code
    $user = $userRepository->find(/* lookup criteria */);
    $message = $messageFactory->create(/* data */);
    // Could wrap in try/catch
    $messageService->sendUserMessage($user,$message);
    

    Been working with Doctrine2 as well. Your domain entity objects are just that objects…they should not have any idea of where they came from, the domain model just manages them and passes them around to the various functions that manage and manipulate them.

    Looking back over, I’m not sure that I completely answered your question. However, I don’t think that the entities themselves should have any access to the mappers. Create Services/Repositories/Whatever to operate on the objects and utilize the appropriate techniques in those functions…

    Don’t overengineer it from the onset either. Keep your domain focused on its goal and refactor when performance is actually an issue.

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

Sidebar

Related Questions

No related questions found

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.