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

Find a better way to handle many_many relation updates #5

Open
maxime-rainville opened this issue Dec 15, 2024 · 0 comments
Open

Find a better way to handle many_many relation updates #5

maxime-rainville opened this issue Dec 15, 2024 · 0 comments

Comments

@maxime-rainville
Copy link
Member

DataObjectEvent doesn't really work with many_many relation updates because it doesn't trigger a write event.

We managed to partially work around this by putting a custom accessor on the many_many relation and firing a custom event. But that's not really a solution that scales.

    public function ProfessionalCategoryPages(int|array $ids = null): Relation
    {
        $proID = $this->ID;
        $callback = function () use ($proID) {
            EventService::singleton()->dispatch(new ProfessionalCategoryPageAssociation($proID));
        };
        $relation = $this->getManyManyComponents('ProfessionalCategoryPages', $ids);

        // If we are working with an unsaved professional, we might get an UnsavedRelationList
        if ($relation instanceof ManyManyList) {
            // These callbacks will be fired when an item is removed or added to the relation
            $relation->addCallbacks()->add($callback);
            $relation->removeCallbacks()->add($callback);
        }

        return $relation;
    }

If our many_many relations were handled as many_many_through, we could just target the pivot DataObject. Maybe that's the ultimate recommended solution.

After a quick look at ManyManyList or RelationList or DataList, I couldn't find any hooks that could be targeted to figure when an object is added or removed.

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