DDC-1175: Call chaining for entity manager #1477

Closed
opened 2026-01-22 13:15:33 +01:00 by admin · 4 comments
Owner

Originally created by @doctrinebot on GitHub (May 27, 2011).

Originally assigned to: @beberlei on GitHub.

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.

Originally created by @doctrinebot on GitHub (May 27, 2011). Originally assigned to: @beberlei on GitHub. 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.
admin added the Improvement label 2026-01-22 13:15:33 +01:00
admin closed this issue 2026-01-22 13:15:34 +01:00
Author
Owner

@doctrinebot commented on GitHub (May 27, 2011):

Comment created by @beberlei:

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

@doctrinebot commented on GitHub (May 27, 2011): Comment created by @beberlei: A fluent interface shouldnt be introduced just for convenience, it makes no sense here semenatically.
Author
Owner

@doctrinebot commented on GitHub (May 30, 2011):

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:

$entity_manager->persist($an_entity)
$entity_manager->persist($another_entity)
$entity_manager->persist($some_entity)
$entity_manager->persist($the_entity)
$entity_manager->remove($little_entity)
$entity_manager->remove($big_entity)
$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 commented on GitHub (May 30, 2011): 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: $entity_manager->persist($an_entity) $entity_manager->persist($another_entity) $entity_manager->persist($some_entity) $entity_manager->persist($the_entity) $entity_manager->remove($little_entity) $entity_manager->remove($big_entity) $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.
Author
Owner

@doctrinebot commented on GitHub (May 30, 2011):

Comment created by @beberlei:

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

@doctrinebot commented on GitHub (May 30, 2011): Comment created by @beberlei: No reason to reopen. We made this decision and its final
Author
Owner

@doctrinebot commented on GitHub (May 30, 2011):

Issue was closed with resolution "Fixed"

@doctrinebot commented on GitHub (May 30, 2011): Issue was closed with resolution "Fixed"
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#1477