Announcement

Collapse
No announcement yet.

beforeSave hook to spawn multiple Tasks

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • beforeSave hook to spawn multiple Tasks

    I am trying to create a hook for the beforeSave event on Tasks so that it spawns multiple, new tasks for a particular task.
    Below are the 'barebones' of it but I am just struggling here!!!
    Help & thanks in advance:-)

    Scott


    <?php
    namespace Espo\Custom\Hooks\Task;
    use Espo\ORM\Entity;
    class Task extends \Espo\Core\Hooks\Base
    {

    public function beforeSave(Entity $entity, array $options = array())
    {
    if($entity->isNew())
    {
    $entityManager = $this->getEntityManager();
    $newhire = $entityManager->getEntity('Task');
    $newhire->set('name', 'Test new hire task1');
    $entityManager->saveEntity($newhire);
    return;
    }
    }
    }

  • #2
    Hello
    In this case you will have a loop
    Try this
    Code:
    public function beforeSave(Entity $entity, array $options = array())
    {
        if($entity->isNew() && empty($option['ignoreHook'])) {
            $entityManager = $this->getEntityManager();
            $newhire = $entityManager->getEntity('Task');
            $newhire->set('name', 'Test new hire task1');
            $entityManager->saveEntity($newhire, ['ignoreHook' => true]); 
        } 
    }
    it will create sub tasks only for first task

    or you can create relation with task and set for children tasks a parent task. And check in hook, if parentTaskId is empty

    Comment


    • #3
      Tanya... thank you very much for your prompt reply! I've read many of the threads here and have been very impressed with yours and Yuri's assistance to the users here:-) As to your solution above, I will implement it later today... the funny thing is I discovered that 'loop' when I tried it on afterSave (which I know is the wrong event) it produced something like 4877 records! Lol

      Comment


      • #4
        Hi Tanya... well I gave it a go but it comes up with an error 503...

        Just so we're clear and I'm not doing something stupid, I have the following code: in this directory -> espo/custom/espo/custom/hooks/task/task.php

        Watcha think?

        Best regards
        Scott


        <?php
        namespace Espo\Custom\Hooks\Task;
        use Espo\ORM\Entity;
        class Task extends \Espo\Core\Hooks\Base
        {
        public function beforeSave(Entity $entity, array $options = array())
        {
        if($entity->isNew() && empty($option['ignoreHook'])) {
        $entityManager = $this->getEntityManager();
        $newhire = $entityManager->getEntity('Task');
        $newhire->set('name', 'Test new hire task1');
        $entityManager->saveEntity($newhire, ['ignoreHook' => true]);
        }
        }
        }

        Comment


        • tanya
          tanya commented
          Editing a comment
          didn't run the code, but ... change $option to $options in if

      • #5
        PS... this is what shows up in the error log
        [2017-10-30 19:30:40] Espo.WARNING:

        E_WARNING: array_diff():

        Argument #1 is not an array {"code":2,"message":"array_diff():

        Argument #1 is not an array","file":"/home/flourxo2/public_html/espo/application/Espo/Core/HookManager.php","line":171,"context":{"hookDirs":["custom/Espo/Custom/Hooks"],"hookData":[],"hookDir":"custom/Espo/Custom/Hooks","fileList":{"Task":["Task.php"]},"scopeName":"Task","hookFiles":["Task.php"],"hookScopeDirPath":"custom/Espo/Custom/Hooks/Task","normalizedScopeName":"Task","scopeHooks":[],"hookFile":"Task.php","hookFilePath":"custom/Espo/Custom/Hooks/Task/Task.php","className":"\\Espo\\Custom\\Hooks\\Task \\Task","classMethods":null}} []


        [2017-10-30 19:30:40] Espo.WARNING:

        E_WARNING: Invalid argument supplied for foreach() {"code":2,"message":"Invalid argument supplied for foreach()","file":"/home/flourxo2/public_html/espo/application/Espo/Core/HookManager.php","line":173,"context":{"hookDirs":["custom/Espo/Custom/Hooks"],"hookData":[],"hookDir":"custom/Espo/Custom/Hooks","fileList":{"Task":["Task.php"]},"scopeName":"Task","hookFiles":["Task.php"],"hookScopeDirPath":"custom/Espo/Custom/Hooks/Task","normalizedScopeName":"Task","scopeHooks":[],"hookFile":"Task.php","hookFilePath":"custom/Espo/Custom/Hooks/Task/Task.php","className":"\\Espo\\Custom\\Hooks\\Task \\Task","classMethods":null,"hookMethods":null}} []
        Last edited by Scott_Smith; 10-31-2017, 08:22 AM. Reason: *** removed excess whitespace in comment

        Comment


        • #6
          Tanya...I got it !

          Typo on:

          public function beforeSave(Entity $entity, array $options = array())
          {
          if($entity->isNew() && empty($option['ignoreHook'])) { <<<<<-options not option

          Thanks for your help!
          Scott

          Comment

          Working...
          X