Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Doctrine without setters

I have been reading about Domain Driven Design and that entities should not have setters. It makes sense to pass the properties in the constructor, so the object is solid. Is there a way to use Doctrine ORM with this practice?

class User
{
    private $firstname;

    public function __construct($firstname)
    {
        $this->firstname = $firstname;
    }
}

How can Doctrine deal with this setup? Any drawbacks?

like image 531
Joe Avatar asked Mar 13 '16 00:03

Joe


1 Answers

You will come across many drawbacks, like:

  • ID is available after persist => solution: using object ids or UUID in entity constructor
    • for more see: https://carlosbuenosvinos.com/doctrine-25-ddd-entities-and-identities/
  • Symfony forms like to use setters => solution: use constructor (as you do) and CQRS
    • for more see:
      • http://verraes.net/2013/04/decoupling-symfony2-forms-from-entities/
      • https://webmozart.io/blog/2015/09/09/value-objects-in-symfony-forms/
  • value objects (like Email object that validates email in it's constructor) => solution: use embeddables
    • for more see: https://www.doctrine-project.org/projects/doctrine-orm/en/latest/tutorials/embeddables.html
like image 69
Tomas Votruba Avatar answered Sep 20 '22 13:09

Tomas Votruba