Skip to content

Loading…

DDC-1175: Call chaining for entity manager #1778

Closed
doctrinebot opened this Issue · 4 comments

2 participants

@doctrinebot

Jira issue originally created by user dukeofgaming:

It would be nice to have the entity manager return $this in methods like persist and remove, so instead of having this:

$em = $this->get('doctrine')->getEntityManager();
$user = $em->find('AcmeHelloBundle:User', $id);
$em->remove($user);
$em->flush();

One could do this:

$em = $this->get('doctrine')->getEntityManager();
$user = $em->find('AcmeHelloBundle:User', $id);
$em
->remove($user)
->flush();

It is a small improvement (and perhaps its not the best case to demonstrate it) but it makes using the entity manager more comfortable.

@doctrinebot

Comment created by @beberlei:

A fluent interface shouldnt be introduced just for convenience, it makes no sense here semenatically.

@doctrinebot

Comment created by dukeofgaming:

Well, I never spoke of any fluent interface, and method chaining —which can be easily confused with fluent interfaces or vice versa (read the Update at the bottom of http://martinfowler.com/bliki/FluentInterface.html)— is often used for convenience, meaning readability and practicality.

So compare:

$entity_manager
->persist($an_entity)
->persist($another_entity)
->persist($some_entity)
->persist($the_entity)
->remove($little_entity)
->remove($big_entity)
->flush();

With:

$entitymanager->persist($anentity)
$entitymanager->persist($anotherentity)
$entitymanager->persist($someentity)
$entitymanager->persist($theentity)
$entitymanager->remove($littleentity)
$entitymanager->remove($bigentity)
$entity_manager->flush();

It is way less tedious and way more readable, and again, not a fluent interface, just method chaining to improve readability and ease of use.

@doctrinebot

Comment created by @beberlei:

No reason to reopen. We made this decision and its final

@doctrinebot

Issue was closed with resolution "Fixed"

@beberlei beberlei was assigned by doctrinebot
@doctrinebot doctrinebot closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.