doctrine/collections
Doctrine Collections provides a lightweight, flexible collection abstraction for PHP. It offers ArrayCollection and collection interfaces with rich filtering, mapping, matching, and criteria-based querying, useful as a foundation for domain models and ORM-friendly data handling.
@foreach loops).ArrayCollection<T>) for better IDE support and runtime safety.Criteria for complex filtering (similar to Eloquent’s where but more expressive).LazyCollection for deferred execution (e.g., paginated API responses).PersistentCollection/ReadableCollection for thread-safe or cacheable data.Illuminate\Support\Collection suffices for basic needs, Doctrine Collections offers strong typing, criteria-based queries, and Doctrine ORM integration (if used alongside Doctrine).doctrine/collections) and use as a drop-in replacement for arrays or Laravel Collections.ArrayCollection<string>), improving maintainability.3.x) are stable, but 3.0.0 introduced BC breaks (e.g., readonly modifier, final classes). Avoid mixing 2.x and 3.x in the same project.Criteria).| Risk Area | Severity | Mitigation Strategy |
|---|---|---|
| BC Breaks (3.0.0+) | High | Pin to 2.6.x for stability; migrate incrementally. |
| Type System Complexity | Medium | Use ArrayCollection for simplicity; reserve generics for critical paths. |
| Laravel Overlap | Low | Prefer Laravel Collections for view/data layer; Doctrine for business logic. |
| ORM Dependency | Low | Only relevant if using Doctrine ORM alongside. |
| PHP 8.4 Requirement | Medium | Upgrade PHP if needed (Laravel 10+ supports PHP 8.4). |
ArrayCollection for arrays; replace Collection::where() with Criteria for complex logic.PersistentCollection)?ArrayCollection for DTOs/API responses (serializes to arrays via toArray()).hasMany/belongsToMany with ArrayCollection for typed relationships.Criteria for filtering (e.g., UserRepository::findByCriteria($criteria)).array() with new ArrayCollection() in services/repositories.// Before
$users = ['Alice', 'Bob'];
// After
$users = new ArrayCollection(['Alice', 'Bob']);
array_filter() with Criteria for complex queries.use Doctrine\Common\Collections\Criteria;
$criteria = Criteria::create()
->where(Criteria::expr()->gt('age', 18))
->orderBy(['name' => 'ASC']);
$adults = $users->matching($criteria);
$orders = new ArrayCollection<Order>($rawOrders);
LazyCollection for large datasets.Collection, but they can coexist:
// Doctrine → Laravel
$laravelCollection = new \Illuminate\Support\Collection($doctrineCollection->toArray());
// Laravel → Doctrine
$doctrineCollection = new ArrayCollection($laravelCollection->all());
PersistentCollection is preferred for entity relationships.2.6.x.UserService).deprecated() helper for array-based methods.Criteria provides a standardized query language.array_filter but more maintainable.LazyCollection enables streaming for large datasets (e.g., paginated APIs).PersistentCollection can be serialized for Redis/Memcached.Criteria can optimize queries when used with Doctrine ORM.| Scenario | Impact | Mitigation |
|---|---|---|
| Type Errors | Runtime crashes if generics misused. | Use PHPStan + strict typing. |
| Criteria Misconfiguration | Silent no-results if query wrong. | Add validation for Criteria. |
| Lazy Loading Failures | Memory leaks if not iterated. | Use count() or toArray() to force evaluation. |
| PHP 8.4 Upgrade Issues | BC breaks in new Laravel versions. | Test on PHP 8.4 early. |
| ORM Inconsistencies | PersistentCollection vs. ArrayCollection conflicts. |
Standardize on one per project. |
ArrayCollection, Criteria).How can I help you explore Laravel packages today?