DDC-128: Consider adding EntityManager#link/unlink methods for direct association manipulation #161

Closed
opened 2026-01-22 12:29:02 +01:00 by admin · 4 comments
Owner

Originally created by @doctrinebot on GitHub (Nov 7, 2009).

Jira issue originally created by user romanb:

A problem when working with collection-valued associations is that almost all operations except add($obj) require the collection to become initialized in order for the operation to be performed properly. While this is all correct and beautiful OO-wise it may be problematic at times with regards to performance. Hence we might want to consider to provide some convenient methods along the lines of link/unlink (name suggestions?) which allow more direct, less OO collection manipulation. Such methods obviously would bypass the normal object lifecycle and the changes done through these methods will not be reflected in the in-memory objects and collections, unless the user keeps them in-synch himself.

Originally created by @doctrinebot on GitHub (Nov 7, 2009). Jira issue originally created by user romanb: A problem when working with collection-valued associations is that almost all operations except add($obj) require the collection to become initialized in order for the operation to be performed properly. While this is all correct and beautiful OO-wise it may be problematic at times with regards to performance. Hence we might want to consider to provide some convenient methods along the lines of link/unlink (name suggestions?) which allow more direct, less OO collection manipulation. Such methods obviously would bypass the normal object lifecycle and the changes done through these methods will not be reflected in the in-memory objects and collections, unless the user keeps them in-synch himself.
admin added the New Feature label 2026-01-22 12:29:02 +01:00
admin closed this issue 2026-01-22 12:29:03 +01:00
Author
Owner

@doctrinebot commented on GitHub (Nov 7, 2009):

@doctrinebot commented on GitHub (Nov 7, 2009): - is referenced by [DDC-546: New fetch mode EXTRA_LAZY for collections](http://www.doctrine-project.org/jira/browse/DDC-546)
Author
Owner

@doctrinebot commented on GitHub (Dec 11, 2009):

Comment created by @beberlei:

Questions

  • I suppose link and unlinked entities would then handled by UnitOfwork commit also?
  • Since the collection is not initialized, one does not know upfront if the action will be successful, what happens if:
    **** an entity is linked with a collection, although they are already connected.
    **** an entity is unlinked from a collection it is not in.

Regarding the naming, i like link/unlink.

@doctrinebot commented on GitHub (Dec 11, 2009): Comment created by @beberlei: Questions - I suppose link and unlinked entities would then handled by UnitOfwork commit also? - Since the collection is not initialized, one does not know upfront if the action will be successful, what happens if: ***\* an entity is linked with a collection, although they are already connected. ***\* an entity is unlinked from a collection it is not in. Regarding the naming, i like link/unlink.
Author
Owner

@doctrinebot commented on GitHub (Dec 17, 2009):

Comment created by romanb:

What do you mean by "handled by UnitOfWork commit" ? Whether the SQL is "scheduled" or executed immediately? Interesting question.
Scheduling would probably be better but also more difficult.

As far as usage is concerned, I currently imagine it as follows:

// EntityManager#link($sourceObj, $field, $targetObj)
$user = $em->getReference($userId); // $userId probably from request parameters
$address = $em->getReference($addressId); // $addressId probably from request parameters
$em->link($user, 'addresses', $address);

"What happens if: an entity is linked with a collection, although they are already connected."

Probably an SQL error which results in an exception from the driver. Depends on the database constraints though.

"What happens if: an entity is unlinked from a collection it is not in"

Probably nothing, at least not from the SQL side. An exception could be thrown from Doctrine itself if the update affected 0 rows.

Thanks for these initial questions. Thats definitely food for thought. Keep it coming.

@doctrinebot commented on GitHub (Dec 17, 2009): Comment created by romanb: What do you mean by "handled by UnitOfWork commit" ? Whether the SQL is "scheduled" or executed immediately? Interesting question. Scheduling would probably be better but also more difficult. As far as usage is concerned, I currently imagine it as follows: ``` // EntityManager#link($sourceObj, $field, $targetObj) $user = $em->getReference($userId); // $userId probably from request parameters $address = $em->getReference($addressId); // $addressId probably from request parameters $em->link($user, 'addresses', $address); ``` "What happens if: an entity is linked with a collection, although they are already connected." Probably an SQL error which results in an exception from the driver. Depends on the database constraints though. "What happens if: an entity is unlinked from a collection it is not in" Probably nothing, at least not from the SQL side. An exception could be thrown from Doctrine itself if the update affected 0 rows. Thanks for these initial questions. Thats definitely food for thought. Keep it coming.
Author
Owner

@doctrinebot commented on GitHub (Aug 26, 2010):

Comment created by romanb:

Pushed back.

@doctrinebot commented on GitHub (Aug 26, 2010): Comment created by romanb: Pushed back.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#161