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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:35:08+00:00 2026-06-17T08:35:08+00:00

I have users and groups. Each user can have any number of groups. I

  • 0

I have users and groups. Each user can have any number of groups. I want to show user groups in paginated form, n groups at a time. I know how to implement an ordinary pagination, however I don’t know how to integrate it into my Domain Driven Design (and without leading to code duplication later). I would like it to work something like this:

$adapter = new DatabaseAdapter(...);
$userRepository = new UserRepository($adapter);
$user = $userRepository->fetchById(1);
$groups = $user->getGroups()->getRange($offset, $limit);

and the same for other domain entities:

$projects = $user->getProjects()->getRange($offset, $limit);
...

Simplified, my code looks like this:

class Group
{
    private $_id;
    private $_name;

    public function __construct($id, $name) {
        $this->setId($id);
        $this->setName($name);
    }

    public function setId() {
        $this->_id = $id;
    }

    public function getId() {
        return $this->_id;
    }

    public function setName($name) {
        $this->_name = $name
    }

    public function getName() {
        return $this->_name;
    }
}

class Groups
{
    private $_elements = array();

    public function __construct(array $groups) {
        foreach($groups as $group) {
            if(!($group instanceof Group)) {
                throw new Exception();
            }
            $this->_elements[] = $group;
        }
    }

    public function toArray() {
        return $this->_elements;
    }
}

class GroupMapper
{
    private $_adapter;

    public function __construct(DatabaseAdapterInterface $adapter) {
        $this->_adapter = adapter;
    }

    public function fetchById($id) {
        $row = $this->_adapter->select(...)->fetch();
        return $this->createEntity($row);            
    }

    public function fetchAll() {
        $rows = $this->_adapter->select(...)->fetchAll();
        return $this->createEntityCollection($rows);
    }

    private function createEntityCollection(array $rows) {
        $collection = array();
        foreach($rows as $row) {
            $collection[] = $this->createEntity($row);
        }
        return $collection;
    }

    private function createEntity(array $row) {
        return new Group($row['id'], $row['name']);
    }
}


class User
{
    private $_id;
    private $_name;
    private $_groups;

    public function getId() {
        return $this->_id;
    }

    public function setName($name) {
        $this->_name = $name;
    }

    public function getName() {
        return $this->_name;
    }

    public function setGroups($groups) {
        $this->_groups = $groups;
    }

    public function getGroups() {
        return $this->_grous;
    }
}

class UserRepository
{
    private $_userMapper = null;
    private $_groupMapper = null;

    public function __construct(DatabaseAdapterInterface $adapter) {
        $this->_groupMapper = new GroupMapper($adapter);
        $this->_userMapper = new UserMapper($adapter);
    }

    public function fetchById($id) {
        $user = $this->_userMapper->fetchById($id);
        if($user) {
            $groups = $this->_groupMapper->fetchAllByUser($id);
            $user->setGroups($groups);
        }
    }

    public function fetchAll() {
        ...
    }
}

Thank you for your help!

  • 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-17T08:35:09+00:00Added an answer on June 17, 2026 at 8:35 am

    In DDD this is usually implemented by providing paginated query methods directly on the repository. If a group is strictly a child entity of the User aggregate, then just add a query method to the user repository. If a group is its own aggregate, add that method to the group repository. Also, this is a strictly read-only scenario that is used, probably exclusively, for a UI so its not part of the core domain – it is more of a technical concern.

    The alternative is to use an ORM that can inject a collection proxy which executes commands against the database behind the scenes. These approaches are usually more trouble than they’re worth and make the domain code more difficult to reason about and maintain.

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

Sidebar

Related Questions

On my site I have user profiles. Each user profile can have a number
There are many users, many groups that users can be part of. Each group
I have 3 tables users(id,name),groups(id,name) and users_groups(user_id,group_id). users and groups have many to many
I have a many-to-many relationship between users and groups and I have a table
TO BE CLEAR: I have two groups of users - GroupA - User1, User2
[I have posted this at the Django users | Google Groups also.] Using the
I have class that represents users. Users are divided into two groups with different
I have three tables specifying important columns below Users(Id, username) Groups(Id, groupname, creator) (creator
Say I have a voting table, where users can vote on values up, down,
I'm trying to run a query which counts the number of orders each user

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.