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

Be more frugal about updating relationships #1606

Closed
jrjohnson opened this issue Sep 30, 2016 · 2 comments
Closed

Be more frugal about updating relationships #1606

jrjohnson opened this issue Sep 30, 2016 · 2 comments
Assignees
Labels

Comments

@jrjohnson
Copy link
Member

Anytime an API request comes in for an endpoint we clear all of the relationships and then rebuild them from the sent data. Instead we should only change those items which have changed. This may improve some deadlock issue and will also have the benefit of only placing actual changes into the audit log. Currently every request indicates that it has changed relationships.

This might look like:

  /**
     * @param Collection $users
     */
    public function setUsers(Collection $users = null)
    {
        if (is_null($users)) {
            $this->users = new ArrayCollection();
            return;
        }

        $existingUsers = $this->getUsers();
        $usersToDelete = $existingUsers->filter(function (User $user) use ($users) {
            return !$users->contains($user);
        });
        $usersToAdd = $users->filter(function (User $user) use ($existingUsers) {
            return !$existingUsers->contains($user);
        });

        foreach ($usersToDelete as $user) {
            $this->removeUser($user);
        }

        foreach ($usersToAdd as $user) {
            $this->addUser($user);
        }
    }

Much of this can likely be generalized into a trait to limit code duplication.

@jrjohnson
Copy link
Member Author

Turns out the symfony will handle this for us if we add removeer methods to each entity. Then forms will no longer call set they will just call that method.

@jrjohnson
Copy link
Member Author

Fixed in #1611

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant