doctrine/collections
Doctrine Collections is a lightweight abstraction for working with arrays and object sets in PHP. Provides Collection interfaces and implementations like ArrayCollection, plus filtering, mapping, criteria-based matching, and iteration utilities used across Doctrine projects.
Strengths:
ArrayCollection, Criteria, ExpressionBuilder), aligning with modern PHP (8.4+) and Laravel’s dependency injection patterns.HasMany, MorphMany, etc.) and service containers, as it’s a core dependency in the Doctrine ecosystem.Criteria, ExpressionBuilder) for filtering, sorting, and aggregating data without raw SQL, reducing boilerplate.readonly modifier adoption), with clear deprecation cycles.Weaknesses:
map, filter), native PHP arrays or Illuminate\Support\Collection may suffice, adding unnecessary complexity.Criteria, CompositeExpression) require understanding Doctrine’s query DSL, which may not align with Laravel’s query builder (where, orderBy).PersistentCollection) are ORM-specific and irrelevant for non-Doctrine Laravel apps.Illuminate\Support\Collection; the two can coexist (e.g., Doctrine Collections for ORM/data layers, Laravel Collections for view/presentation).CollectionInterface) for dependency injection.belongsToMany) already use Doctrine Collections internally. Adopting it explicitly could standardize collection handling across the app.Criteria for complex filters).LazyCollection) reduces memory usage for large datasets.foreach ($doctrineCollection as $item) works, but array_map won’t).readonly and final classes, which may require updating extending classes (e.g., custom collection types).MockCollection or Criteria-based test data).Illuminate\Support\Collection or native PHP arrays suffice for the target use case?3.1.x) to avoid BC breaks, or accept patch updates for bug fixes?ArrayCollection or Criteria? If so, ensure compatibility with final classes in 3.0.0+.ArrayCollection in repositories/models (e.g., return new ArrayCollection($results)).Criteria for request-based filtering (e.g., GET /users?role=admin → Criteria::create()->where(...)).CollectionInterface instead of array or Collection).readonly properties and final classes for safer collections.ArrayCollection<User>) with PHP 8.0+ return types.array() with new ArrayCollection() in constructors.Criteria for complex queries instead of manual where clauses.ArrayCollection (e.g., findAll() → findAllCollection()).CollectionInterface in method signatures for flexibility.MockCollection or Criteria-based factories.CollectionInterface in interfaces (e.g., UserRepositoryInterface).PaginatedCollection) for domain-specific needs.Collection, so they can’t use Laravel’s methods (e.g., pluck(), toJson()). Workarounds:
$laravelCollection = new \Illuminate\Support\Collection($doctrineCollection->toArray()).DoctrineToLaravelCollection).PersistentCollection for ORM-specific logic (e.g., SoftDeletableCollection).doctrine/orm, api-platform/core). Conflicts are unlikely but should be tested.| Priority | Task | Dependencies |
|---|---|---|
| High | Replace raw arrays in repositories with ArrayCollection. |
None |
| High | Migrate complex queries to Criteria. |
Doctrine Collections v3.0+ |
| Medium | Update controllers to accept CollectionInterface. |
Phase 1 completion |
| Medium | Add custom collection classes (e.g., PaginatedCollection). |
Core collection adoption |
| Low | Bridge Doctrine Collections to Laravel Collections where needed. | Phase 2 completion |
| Low | Deprecate raw array returns in public APIs. | Full standardization |
filter(), map(), and reduce() eliminate manual loops.readonly changes).add()) is restricted due to final classes in 3.0.0+.How can I help you explore Laravel packages today?