Add merge method to ArrayCollection
Feature Request
The ArrayCollection-on class should have a merge method
Why
When I want to add ManyToMany items, the doctrine deletes them first and then adds them, but this can be used to prevent it from deleting what we don't want to delete
How
public function merge(Collection $collection): self
{
foreach ($this->getValues() as $element) {
if (!$collection->contains($element)) {
$this->removeElement($element);
}
}
foreach ($collection as $element) {
if (!$this->contains($element)) {
$this->add($element);
}
}
return $this;
}
This should be opened on doctrine/collections instead
I made my suggestion for this in the PR https://github.com/doctrine/collections/pull/462
The ArrayCollection is just the default implementation of the collection contracts. I don't believe that we need functionality to that class that is not supported by any other collection.
Also, would this merge function do anything beyond the following?
return new ArrayCollection(array_merge(
$collectionA->toArray(),
$collectionB->toArray(),
));
If not, I'd like to close this issue.
Closing as explained.