Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add merge method to ArrayCollection #1030

Open
dozsan opened this issue Mar 10, 2025 · 0 comments
Open

Add merge method to ArrayCollection #1030

dozsan opened this issue Mar 10, 2025 · 0 comments

Comments

@dozsan
Copy link

dozsan commented Mar 10, 2025

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;
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant