Syncing a ManyManyList

Sometimes you want to update a ManyMany relation to have exact this set of foreign keys. Not just adding and deleting, but both - best in just one call. But ManyManyList itself doesn't come with this functionality out of the box. Really?

Traversing all parent classes of ManyManyList we can see it's a subclass of DataList, which comes with setByIDList(array $idList) which can do the job for us.

$this->MyManyManyRelation()->setByIDList([1,2,7])

will add all needed items and remove all items from the relation table that are not in the list of IDs.

As DataList doesn't know anything about $manymany_extra_fields it cannot handle them.

Thanks to marvanni for asking this topic on our slack SilverStripe community channel.

Rate this post (1 ratings)

Post your comment

Comments

No one has commented on this page yet.

RSS feed for comments on this page | RSS feed for all comments