Like what we’re doing? Star us on GitHub!

cyclic dependencies between collection - hooks produce endless loop

zaunermax
last year
1 1

Hello again! I am really loving payload cms so far 🙌 We encountered one problem though:

Is there a way to specifically turn off hooks on a CRUD request? Our usecase is that we have a cyclic dependency between let's say categories and meta categories. When saving either of them we wanted to build a hook system, which automagically updates meta categories when saving categories and vice versa.

Now the problem is, that hooks get triggered in a loop, as one collection saves the other and the other way round. This could be mitigated by specifically saving an entity w/o executing it's hooks, or am I wrong?

Have you already encountered this problem? Thanks in advance!

  • jmikrut
    Payload Team
    last year

    Hey @zaunermax — we really appreciate your interest in Payload. Great to hear that you're enjoying working with it. Got a lot more goodness on the way too.

    What I'd do in this case, is first check if the metaCategory or category already has the relationship that it needs, before actually updating it in your hooks.

    That way, if the proper relationship already exists, you won't update again needlessly, and then the hooks won't re-run. Just need to introduce some type of loop-breaking logic. That's what I'd do.

    This is totally a common thing. Need to break the cycle in one way or another.

    Does that help?

Open the post
Continue the discussion in GitHub
Can't find what you're looking for?
Get help straight from the Payload team with an Enterprise License.Learn More