PHP UK Conference 2016 - Marco Pivetta - Doctrine ORM Good Practices and Tricks

PHP UK Conference · 25K views · 2016 · 58:33

Open on YouTube

Web developmentSoftware architecture & designPython, Ruby & scripting languagestalk

Why Stack Overflow recommends it

Stack Overflow users link it when answering Doctrine and Symfony entity design questions, especially about immutable entities, rich domain models, and setting IDs in constructors instead of relying on the database.

Recommended by 4 people across 3 different months, 2016 to 2017.

What it is

A conference talk from PHP UK 2016 in which Doctrine contributor Marco Pivetta reviews how to use the Doctrine ORM well. He covers the pros and cons of different application design approaches with a data mapper and sets out best practices for projects.

What people said

Recommends snsanich · 2016-03 · Stack Overflow · on “How to define current timestamp in yaml with doctrine?”

I suggest not to use "default" for timestamp at all. It will bring unpredictable state in yaml in your application. This video ( PHP UK Conference 2016 - Marco Pivetta - Doctrine ORM Good Practices and Tricks [this video] ) provides some more information about this topic. I suggest you to to go through it and create a named constructor. public function createTimestamp(string $priority, int $priorityNormalized) { $this->priority = $priority; $this->priorityNormalized = $priorityNormalized; } I suggest to be stateless, good luck!

Recommends AphonopelmaChalcodes · 2016-04 · Stack Overflow · on “Doctrine2 immutable entities and append only data structures”

I like the technique described by the Marco Pivetta at PHP UK Conference 2016 ( [this video] ), he recommends to favour immutable entities and instead of changing data structures - appending them . History of changes as a bonus is a nice thing to have for many different reasons, so I would like to apply this approach on my projects. Let's have a look at the following use case: class Task { protected $id; /** * Status[] */ protected $statusChanges; public function __construct() { $this->id = Uuid::uuid4(); $this->statusChange = new ArrayCollection(); } public function changeStatus($status, $user){ $this->statusChange->add(new Status($status, $user, $this); } public function getStatus() { return $t

Recommends E.K. · 2017-04 · Stack Overflow · on “Symfony : is it bad practice to add custom functions in an Entity?”

It's not a bad practice, if your code still satisfies SOLID principles (mainly, Single Responsibility principe in that case) So, if the method isn't related to entity logic (for example, sending emails or persisting something to database right from your entity) -- it's wrong. Otherwise, it's absolutely OK. The major attribute of the logic related to entity -- it should be in the same layer with another stuff in the entity. Actually, Doctrine entities is not just Data Transfer Objects (without behavior). Doctrine's developers insist using the entities as Rich Models (look the video [this video] by Marco Pivetta, one of Doctrine's developers and see his nice presentation [LINK] )

Recommends Đuro Mandinić · 2017-04 · Stack Overflow · on “symfony/doctrine: cannot use object without refreshing”

but I can't access nested object Did you set those assetClasses in the first place? When you work with objects in memory (before persist), you can add and set all nested objects, and use those while still in memory. My guess is that you believe that you need to store objects to database in order for them to get their IDs assigned. IMHO, that is a bad practice and often causes problems. You can use ramsey/uuid library instead, and set IDs in Entity constructor: public function __construct() { $this->id = Uuid::uuid4(); } A database should be used only as a means for storing data. No business logic should be there. I would recommend this video [this video] on Doctrine good practices, and about the