Update the Entity in afterSave hook

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • aldisa
    Junior Member
    • Jun 2023
    • 28

    Update the Entity in afterSave hook

    I am creating a custom module:

    Code:
    namespace Espo\Modules\MyModule;
    In this module, I have created an `afterSave` hook:


    Code:
    <root>
    |-- custom
        |-- Espo
            |-- Modules
                |-- MyModule
                    |-- Hooks
                        |-- Contact
                             |-- MyHook.php

    MyHook.php
    Code:
    public function afterSave(Entity $entity, SaveOptions $options): void
    {
        \\ my code here
    }
    My question is this:

    If I do an update to the Entity in my hook
    Code:
    $entityManager->saveEntity($entity)
    this will trigger an endless loop.

    If I use the SKIP_HOOKS save option
    Code:
    $entityManager->saveEntity($entity, [SaveOption::SKIP_HOOKS => true]);
    then all Workflows and Hooks will not trigger.

    How can I update the Entity in my afterSave hook and skip ONLY my hook and let all other workflows trigger afterSave?
  • yuri
    Member
    • Mar 2014
    • 8627

    #2
    pass any custom option to the saveEntity method and check this option. If it's set, then don't proceed.
    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

    Working...