mirror of
https://github.com/doctrine/orm.git
synced 2026-03-24 06:52:09 +01:00
ArrayCollection/PersistentCollection Architectural Disagreement #5692
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @WyrdNexus on GitHub (Sep 7, 2017).
While Doctrine will provide either an ArrayCollection, or a PersistentCollection, depending on the state of the Entity, no common Class or Interface reflects their shared methods.
More specifically, when defining an entity's collection getter, the return type cannot be specified.
Options include:
This missing common agreement complicates all code implementing these objects, which otherwise could be clean and simple.
For the sake of clarity, consider the PHP7 return type declaration on the getter for such a method.
Both of the following week result in errors:
pubic method getOneToMany(): ArrayCollection
pubic method getOneToMany(): PersistentCollection
The best alternative is:
pubic method getOneToMany(): Collection
... leaving out the method: matching.
@WyrdNexus commented on GitHub (Sep 7, 2017):
As a simple suggestion, perhaps Collection should implement Selectable, and the whole issue disappears.
@Majkl578 commented on GitHub (Sep 7, 2017):
This is true unfortunately.
Impossible due BC, would be a BC break -- only possible in next major of doctrine/collections.
Also possibly not a wise idea -- consider this architecture more in general / different scenarios outside ORM. it doesn't always make sense to have selectable collection.
Regarding ORM, this could be only solved in 3.x somehow, if you have any proposals, ideally without breaking doctrine/collections, you're welcome to send PR for that or discuss here. 👍
@WyrdNexus commented on GitHub (Sep 8, 2017):
Notation Key:
Currently
Proposed
Result
As this would neither remove nor alter any methods, classes, or interface implementations, I fail to see how this would cause a BC break. The only real limitation here, is that it would require two paired PRs against the Collections repository and the ORM Repository.
@Majkl578 commented on GitHub (Sep 8, 2017):
In your proposal, Collection suddenly extends Selectable, thus requires implementation for
matching(). This method previously did not exist in the Collection interface so any code implementing (just) Collection is now suddenly required to also implement newmatching()method, which is a BC break.@beberlei commented on GitHub (Sep 16, 2017):
The
Selectableinterface was added a long time afterCollectionwas finalized. A change to this interface would have been a BC break.The solution would be that "collections" introduces a third interface
SelectableCollectionthat extends from Collection and Selectable and to change bothArrayCollectionandPersistentCollectionto use that.This must be done in two steps:
1.) Add
SelectableCollectionto "doctrine/collections", haveArrayCollectionextend it and release a new version (minor bump enough, because its not a BC break).2.) Bump dependency of "doctrine/orm" to new "doctrine/collections" version and change
PersistentCollection.@michsk commented on GitHub (Nov 8, 2017):
@WyrdNexus did you figure out a workaround for this issue? (without dropping the returntype typehint)
@alcaeus commented on GitHub (Nov 8, 2017):
Please don't forget MongoDB ODM, where
PersistentCollectiondoesn't implementSelectable.@stof commented on GitHub (Mar 7, 2018):
@alcaeus if a new interface is created, there is no issue for the ODM, as Collection would still not require implementing Selectable.
@alcaeus commented on GitHub (Mar 7, 2018):
@stof I was replying to @WyrdNexus who suggested that the
Collectioninterface extendsSelectable, withPersistentCollectionimplementingCollection: this would indeed also affect ODM. A new, separate interface (SelectableCollection extends Selectable, Collection) would not directly impact ODM.@ThomasDupont commented on GitHub (May 14, 2018):
How do you thing about toArray() Method?
I had this issue on my documents and corrected the bug with that
Doctrine will parse the array before insert and The document could be manipulate without The Collection type hinting
@trickreich commented on GitHub (Aug 22, 2018):
Any news/plans on this? This is really frustrating when you are working with PHPStan.
@alcaeus commented on GitHub (Aug 22, 2018):
If you use
phpstan/phpstan-doctrineit contains an extension to pretend that theCollectioninterface contains amatchingmethod. As long as you're not using MongoDB ODM, you might be safe unless you've created custom collection implementations.The other alternative is to annotate return types using a union type (
Collection&Selectable) - PHPStan will understand this, but other tools (notably PhpStorm) may not.@greg0ire commented on GitHub (Jan 14, 2026):
I'm attempting to address the third bullet point with https://github.com/doctrine/collections/pull/518