Hooks not triggered from Service class save/saveEntity function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mixerito
    Junior Member
    • Aug 2022
    • 8

    Hooks not triggered from Service class save/saveEntity function

    I have custom Hook file in custom/Espo/Custom/xxx/
    (beforeSave, afterSave)

    When editing in UI, hooks are normally triggered.

    But when I use:
    $this->entityManager->saveEntity($entity, $options)
    or
    $this->getRepository($entityType)->save($entity, $options);

    in backend files (custom/Espo/Custom/Service/xxx.php),
    these hooks are not triggered !!!
    • The changes to DB are saved correctly,
    • Log file is empty
    • tried with $options = []
    • tried with $options = ['skipHooks' => false]

    The call sequence is following:
    1a) Dropdown action in detail view or
    1b) Mass action in list view
    2) client/custom/src/xxx-handler.js ->actionDoSomething -> Espo.Ajax.postRequest()
    3) route to xxx controller action
    4) Controller class function postActionDoSomething is calling Service class function
    5) Service class (extends \Espo\Core\Templates\Services\Base) function is calling
    $this->entityManager->saveEntity($entity, []);


    My questions:
    1. Is this intended behavior or bug ?
    2. What is the proper way to apply some changes in Mass or Detail view functions with respecting all the hooks ?
  • yuri
    Member
    • Mar 2014
    • 8627

    #2
    Supposed to be triggered, no matter from where you call. Try to debug starting from your service class.
    If you find EspoCRM good, we would greatly appreciate if you could give the project a star on GitHub. We believe our work truly deserves more recognition. Thanks.

    Comment

    • AgentT
      Member
      • Aug 2021
      • 77

      #3
      From my experience, the service hooks (beforeCreateEntity for example) will be triggered only for any updates made through the UI and not via ORM saves/updates or API calls (which are ORM saves/updates). But beforeSave under the Hooks/xxxxx.php will trigger all the time.

      Is this by design or a bug?

      Comment

      • yuri
        Member
        • Mar 2014
        • 8627

        #4
        I assumed the author created a regular hook (that works on ORM level). Service/Record hooks won't work for sure.
        If you find EspoCRM good, we would greatly appreciate if you could give the project a star on GitHub. We believe our work truly deserves more recognition. Thanks.

        Comment

        • mixerito
          Junior Member
          • Aug 2022
          • 8

          #5
          I am sorry for the confusion and thank you for your fast responses.

          I have found the problem.

          I have changed the name of Hook .php files, but did not change the class names inside respectively.
          From that moment I suppose, the system stopped to process those.

          There must have been some cache reason for continuing to work from UI. (even though I cleared the cache and did rebuild after each change)

          Comment

          • AgentT
            Member
            • Aug 2021
            • 77

            #6
            Note that if you are using OpCache, you will have to clear that as well before testing out the code changes.

            Comment

            Working...