get_linked_beans in after retrive logik hook


If I call get_linked_beans method in the after retrive logik hook with N linked beans, it is called 1 + N times (N = amount of related records). For example:

function afterRetrieve($bean, $args, $events)

    $xBean = $bean;

    $yBeans = $xBean->get_linked_beans(

    $GLOBALS['log']->fatal("I Was Called");


If I have 2 linked records in yBeen, in logfile it will be 3 records “I Was Called”.

So, my question is: How i can get linked beans with only one call from after retrive logic hook, may be it better to use SQL?

(without get_linked_beans or if linked beans = 0 in logfile only one record “I Was Called” )


I ma not sure, but maybe something from this article might be of help:

If you can get some condition to distinguish the original invocation of the hook from the others…

1 Like

Thank you, but this describes problem with saving. I am not saving. To test I did next steps.

  1. Clean Install CRM
  2. With module builder create new package
  3. With module builder create new module1
  4. With module builder create new module2
  5. With module builder create relationship one-to-many (module1 -> module2)
  6. Create After Retrieve Hook on module 1
  7. Call get_linked_beans(‘module2’)
  8. There is 3 calls After Retrieve Hook

New details. No matter how many records in a related entity when I call get_linked_beans, after retrieve hook called 3 times instead of 1. It looks like a bug.

Yes, it might be a bug…

You can try using debug_backtrace or debug_print_backtrace in each of those calls into the hook, to get a view of what the call stack looks like in each of those calls. I am not sure, but this might give you some clues as to which parts of the code are calling into the hook.

1 Like

Second call is in the fill_in_relationshipfieds() from related records

So, it seems that the hook is called once for the record, and then once for each “relate” field in the record. Maybe it’s not a bug, it seems to be by design.

The question is: when you are inside the hook, can you distinguish between these calls? Can you find some parameter that you can test, to know which case it is?

1 Like

I found sulution with static parameter in the class. And can use prevent duplication of logic in hook.

But, i cant prevent calling the retrieve() method (it do query to db, so it affects performance).

1 Like