How to merge two php Doctrine 2 ArrayCollection()

How to merge two php Doctrine 2 ArrayCollection()

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

The concat Method for Doctrine ArrayCollection

You asked if there is a convenience method to merge two Doctrine ArrayCollection instances without iterating manually. The answer is yes, and it is exactly what you are looking for: the concat() method.

Doctrine 2's ArrayCollection implements the ArrayAccess, Countable, and IteratorAggregate interfaces, but its specific utility comes from extending SplObjectStorage in older versions or simply acting as a specialized array wrapper in Doctrine ORM contexts. In modern Doctrine implementations, ArrayCollection exposes a concat() method that accepts another collection (or an array) and appends all elements to the current instance.

Here is the direct implementation matching your request:

use Doctrine\Common\Collections\ArrayCollection;

$collection1 = new ArrayCollection();
$collection2 = new ArrayCollection();

// Populate first collection
$collection1->add($obj1);
$collection1->add($obj2);
$collection1->add($obj3);

// Populate second collection
$collection2->add($obj4);
$collection2->add($obj5);
$collection2->add($obj6);

// Merge them in one line
$collection1->concat($collection2);

// $collection1 now contains {$obj1, $obj2, $obj3, $obj4, $obj5, $obj6}

This approach eliminates the need for a foreach loop where you would manually call $collection1->add($item) for every element in the second collection. The underlying implementation of concat() iterates over the source collection internally and adds the items to the target collection efficiently.

Handling Duplicate Keys and Identity

While merging collections sounds straightforward, there are edge cases worth considering regarding how ArrayCollection handles identity versus keys. Unlike a standard PHP array where array_merge might re-key numeric indices or overwrite values based on key collisions, ArrayCollection treats objects by their instance identity (hash of the object) when used as keys in certain contexts, or simply appends to the end of the internal list if no specific key mapping is required.

If your collections contain entities with primary keys that happen to match existing entries, you must remember that concat() does not perform a merge based on ID; it performs an append. If you are managing unique identifiers and need to prevent duplicates, concat() alone will not filter them out. You would still need to manually check for existence before adding if duplicate prevention is part of your logic.

Performance Considerations

Using concat() is generally more performant than a manual loop because the internal logic resides within the Doctrine library, avoiding the overhead of PHP's native iteration and method calls in your application code. However, this comes with a trade-off: ArrayCollection is not optimized for high-frequency random access or massive dataset manipulation compared to raw arrays or specialized data structures like those provided by the doctrine/dbal abstraction layer for SQL queries.

If you are dealing with millions of records, concatenating collections in memory before persisting them should be done with caution. The method modifies the internal array representation directly. If the resulting collection exceeds memory limits, the operation will fail regardless of whether you used concat() or a loop. Always ensure that the total size of the merged collection fits within your application's memory constraints.

When to Use Collections vs. Arrays

Finally, consider why you are using ArrayCollection in the first place. If you are simply aggregating data for a temporary operation and do not need Doctrine-specific features like change tracking or serialization support, a standard PHP array might suffice. However, if your codebase relies on Doctrine's collection API for consistency, concat() ensures that the merged result remains an ArrayCollection, preserving any type hints or expectations in your downstream logic.

For projects built on top of Laravel, you might find similar utility methods provided by third-party packages or community extensions, but sticking to the core Doctrine 2 API via concat() is usually the most reliable approach for merging collections without introducing external dependencies.

Our network