Skip to content

Separate the domain model and persistence layer #7

@mohammadhsn

Description

@mohammadhsn

Hi,

I have a design-related question, As I know about DDD and some sources I read, most people indicate separations between domain and persistence (or infrastructure in general). Most people use Doctrine ORM when they consider the DDD approach in PHP, domain models (or Entities) are a part of domain, so they have no idea about persistence.

Let's imagine we have an Author domain model that publishes posts, I thought below code:

class Post
{
    public function __construct(
        protected Author $author,
        protected PostTitle $title,
        protected PostContent $content,
        protected DateTimeImmutable $publishedAt
    ) {}
}

class Author
{
    public function __construct(
        protected AuthorEmail $email,
        protected array $posts = []
    ) {}

    public function publishNewPost(Post $post)
    {
        $this->posts []= $posts;
    }
}

I fact, what I want is I could use the Post and Author classes as domain models without any persistence concern (doctrine annotations/ArrayCollection class/...). I'd like to separate Doctrine's entity from domain models and I don't know what should I do. Could you help me, please?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions