If a logic hook stopped working

This is a tricky issue and I wanted to share a solution

I had an after_save logic hook (Hook A) that stopped working after I added an after_relationship_add logic hook (Hook B).

It turned out that the problem was that I had the same infinite loop flag in both Hook A and Hook B called $bean->ignore_update_c

So when Hook A would execute it would set $bean->ignore_update_c = true and then it would get to Hook B it would see that $bean->ignore_update_c is already true and not do anything

This is not something that’s mentioned when we talk about infinite loop preventions, but something important to keep in mind. Am I missing something or the correct solution was to make sure that logic hooks use a unique name for that infinite loop flags? So Hook A uses “$bean->ignore_update_c” and Hook B “$bean->ignore_unique_name”

Made a quick video overview of what I mean here: hopefully it will save someone a bunch of time

1 Like

I normally do this with a static variable to check the repetition, not with a bean variable.

https://www.sugaroutfitters.com/blog/prevent-a-logic-hook-from-running-multiple-times

Hey pgr, thank you! I saw the same article and was thinking of using that also, but since it was mentioned in the official tutorial and in one of Jim’s blog posts I continued using the bean variables.

I guess I’ll start using that method as well. I’m guessing it eliminates any problems like that one that I had, right?

I’m not sure, I never ran into your scenario. But at least it makes it very easy to simply add a second variable to track the different hook.

1 Like

Yep, I just tested it and it works. I’m guessing it’s the static variable of the individual logic hook class and not the bean class, so that’s why it doesn’t get shared between classes (which is what was causing my issue)

1 Like