doctrine/collections
Doctrine Collections is a PHP library providing a flexible abstraction for working with collections. It offers array-like data structures, filtering and matching utilities, and common collection operations used across Doctrine projects and standalone apps.
Start by installing the package via Composer: composer require doctrine/collections. The core interface is Doctrine\Common\Collections\Collection, with ArrayCollection as the primary implementation. The simplest use case is replacing native PHP arrays with ArrayCollection for object collections that require iteration, filtering, or sorting—e.g., managing a set of entities in domain models or value objects. The Collection::fromArray() factory method provides an easy migration path from arrays.
OrderItem[]) in ArrayCollection to enforce immutability or controlled mutation via methods like add(), removeElement(), and remove().Criteria with Matching expressions (ExpressionBuilder) to build SQL-like filters and orderings before materializing results—especially useful in layers before database queries (e.g., in services or VO layers). Example:
$matching = Matching::expr()->eq('status', 'active');
$filtered = $users->matching(Matching::create($matching, Matching::orderBy(['name' => Order::ASC])));
AbstractLazyCollection when deferring expensive collection population until iteration (e.g., lazy-loaded related data).ReadableCollection (or ArrayCollection in 3.x) to expose internal collections safely via getters—ensuring callers can’t mutate internal state directly.final, ReadablesCollection now extends Selectable, and Collection::add() returns void (not true). Ensure tests and extension points are reviewed.filter() returns a new collection, not in-place modification.CompositeExpression::TYPE_OR(). NOT expressions (added in 2.1.0) work but only for equality conditions—not arbitrary predicates.Criteria with orderBy() and setMaxResults() may lose keys; use preserveKeys: true in toArray() or avoid key-dependent logic when slicing.ArrayCollection serializes its internal array—custom logic may be needed if internal state (e.g., hydrated proxies) must be preserved. Always test across cache layers.filter() with key/value arity mismatch) can cause subtle issues.How can I help you explore Laravel packages today?