Isolate flush as small service.
Closed this issue · 3 comments
EntityManager
has fat interface. Try to not rely on it and wrap portion of needed functionality in your specialized interface. For example you could add service:
final class Flush
{
// todo
public function __invoke() {
$this->em->flush();
}
}
and use it in controller as:
public function someAction(Flush $flush) {
// do stuff
$flush();
// send response.
}
What would you recommend in case you need to do some merge logic with $em->remove()
. Isolate it in kind of *Repository
, add final class Remove
service or something else?
remove
is your repository responsibility. But in practice, you rarely need just remove
(you may need archive
for example) but this is other topic.
https://github.com/Arky9782/chat/blob/master/src/AppBundle/Services/Flush.php#L29
Persist should not be there. It's repository responsibility.
Persist - marks entity as managed. Flush - makes diff off all managed entities's state and generate SQL queries as single transaction. Please read about unit of work.