Hook loops

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Kirill
    Junior Member
    • Oct 2018
    • 20

    Hook loops


    The hook should run once after saving, but runs four times. Code of the Hook:
    Хук должен запускаться только один раз, после сохранения, но запускается четыре раза. Код хука:

    PHP Code:
    <?php
    namespace Espo\Custom\Hooks\Account;
    use Espo\ORM\Entity;
    class AccountNew extends \Espo\Core\Hooks\Base
    {    
        public function afterSave(Entity $entity, array $options = array())
        {
            $GLOBALS['log']->warning('AccountNew - working!');
            if ($entity->isNew()&& empty($entity->get('website')) && empty($options['ignoreHook'])) {
               $GLOBALS['log']->warning('AccountNew - website is empty!');
               $entity->set('website', 'www.website.com');
               $this->getEntityManager()->saveEntity($entity, ['ignoreHook' => true]);
            }
        }
    }
    Log entries after the hook condition is met:
    Записи лога, после выполнения условия хука:

    PHP Code:
    [2019-01-28 07:51:01] Espo.WARNING: AccountNew - working! [] []
    [2019-01-28 07:51:01] Espo.WARNING: AccountNew - website is empty! [] []
    [2019-01-28 07:51:01] Espo.WARNING: AccountNew - working! [] []
    [2019-01-28 07:51:02] Espo.WARNING: AccountNew - working! [] []
    [2019-01-28 07:51:18] Espo.WARNING: AccountNew - working! [] [] 
    
    If there are two hooks, they are launched six or eight times. How to run the hook only once, after saving?
    Если хука два, то они запускаются шесть или восемь раз.Как запускать хук только один раз, после сохранения?
    Last edited by Kirill; 01-28-2019, 09:52 AM.
  • Kirill
    Junior Member
    • Oct 2018
    • 20

    #2

    Disconnected all workflows, the number of repetitions was reduced to two.
    Отключил все рабочие потоки, количество повторений сократилось до двух.
    I save entities in workflows and hooks as follows:
    Сохраняю сущности в рабочих потоках и хуках следующим образом:
    PHP Code:
    $this->getEntityManager()->saveEntity($entity, array('skipWorkflow' => true, 'ignoreHook' => true)); 
    

    Comment

    • tanya
      Senior Member
      • Jun 2014
      • 4308

      #3
      Hi,
      try 'skipAll' option
      When i save an entity in workflow with run service action and that service saves the same entity again, the stream reports a update post twice for the same update.

      Comment

      • Kirill
        Junior Member
        • Oct 2018
        • 20

        #4
        The skipAll option fixed the Hook restart error. Thanks!
        Опция skipAll исправила ошибку зацикливания хука. Благодарю!
        Final code:
        Конечный код:
        PHP Code:
        <?php
        
        namespace Espo\Custom\Hooks\Account;
        
        use Espo\ORM\Entity;
        
        class AccountNew extends \Espo\Core\Hooks\Base
        {    
        
            public function afterSave(Entity $entity, array $options = array())
            {
        
                $GLOBALS['log']->warning('AccountNew - working!');
                if ($entity->isNew()&& empty($entity->get('website'))) { 
                   $GLOBALS['log']->warning('AccountNew - website is empty!');
                   $entity->set('website', 'www.website.com');
                   $this->getEntityManager()->saveEntity($entity, ['skipAll' => true]);
                }
            }
        }

        Comment

        • Kirill
          Junior Member
          • Oct 2018
          • 20

          #5
          I find error in my code it's option - 'ignoreHook'! Must be 'skipHooks'
          PHP Code:
          $this->getEntityManager()->saveEntity($entity, ['skipWorkflow' => true, 'skipHooks' => true]); 
          

          Comment

          • rinorway
            Senior Member
            • Feb 2016
            • 180

            #6
            Thank you for translating in English

            Comment

            Working...