Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom Collections through delegation #11637

Open
beberlei opened this issue Oct 9, 2024 · 1 comment
Open

Custom Collections through delegation #11637

beberlei opened this issue Oct 9, 2024 · 1 comment

Comments

@beberlei
Copy link
Member

beberlei commented Oct 9, 2024

Users regularly want to introduce their own collections. We don't want to expose the Collection interface due to its complexity there is too much risk. MongoDB ODM has a code generated solution for this, for ORM I propose we do this with delegation.

  1. doctrine/collections introduces a DelegateCollection with all methods implemented as final, delegating to $delegatee, and one custom method getInternalDelgatee.
  2. In ORM association configuration, allow optional "collectionType" information. When present, ORM converts with logic:
    • create collection new $assoc['collectionType](new PersistentCollection(...)) everywhere a PersistentCollection is created.
    • introspect collection $col = $col instanceof DelegateeCollection ? $col->getDelegatee() : $col
  3. Improve AttributeDriver to detect that a collection type instanceof DelegateeCollection and set collectionType key automatically.
  4. Precondition: In ORM, if collection types are configured and Composer\PackageVersions for doctrine/collection is not high enough, throw.

Example code:

class CommentCollection extends DelegateCollection
{
	public function getEnabled() {
		return $this->delegatee->filter(fn ($comment) => $comment->isEnabled);
        }
}

class Post
{
        private CommentCollection $comments;

	public function getComments(): CommentCollection
        {
             return $this->comments;
         }
}

Reference:

@stof
Copy link
Member

stof commented Oct 9, 2024

4. Precondition: In ORM, if collection types are configured and Composer\PackageVersions for doctrine/collection is not high enough, throw.

simpler version: bump the min version of the doctrine/collection in the ORM to be the minor version introducing this DelegateCollection.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants