Keep Domain Models Free from Doctrine Mapping and Return Collections from ServiceEntityRepository

I’m working on a Symfony project using Doctrine and I want to separate the database mapping from the domain model, following hexagonal architecture and DDD principles. I want my domain entities to be independent of data storage technologies like Doctrine.

What are the best practices for:

  1. Separating Domain Entities from Doctrine Entities: What are recommended ways to map domain entities to Doctrine entities and keep business logic separate from data storage technology?

    • Specifically, I want the domain models to focus solely on the
      business logic, while the mapping to the database should be handled
      separately in the infrastructure layer.
  2. Implementing Repositories: How should I implement repositories in Symfony to adhere to DDD principles while using Doctrine for CRUD operations?

    • Additionally, I have a repository that extends ServiceEntityRepository, and by default, operations like findAll() return an array. I’m looking for a solution that uses this extended class to return a ArrayCollection instead of an array, without rewriting the logic or creating a custom reflective class.

Here is the project structure I want to use:

  • src
    • Domain
      • Collection
      • Model
      • RepositoryInterface
    • Infrastructure
      • Persistence
        • Doctrine
          • Entity/MappingEntity
          • Repository

Code examples or best practices would be very helpful.

Thanks!