Why won't this hook work?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dev77
    Senior Member
    • Feb 2018
    • 123

    Why won't this hook work?

    Via a hook, I'm trying to 'touch' a file when I update an account record. This is my first attempt to use a hook. The hook does not work. Can anyone tell me why? Here is the code:

    Code:
    <?php
    namespace Espo\Custom\Hooks\Accounts;
    
    use Espo\ORM\Entity;
    
    class MyHook
    {    
        // An optional parameter, defines in which order hooks will be processed.
        // Lesser value means sooner.
        //public static int $order = 5;
    
        //public function __construct(
            // Define needed dependencies.
        //) {}
    
        public function afterSave(Entity $entity, array $options): void
        {
            $filename = "/usr/home/xxxxx/public_html/xxxx.com/XXX-test-php.txt";
           touch($filename);
        }
    }
    
    ​
    Maybe EspoCRM does not let you do anything outside the system via hooks?

    Help is appreciated.
  • rabii
    Active Community Member
    • Jun 2016
    • 1250

    #2
    it won't work because you simply have the wrong namespace it should namespace Espo\Custom\Hooks\Account; (Account) with s.

    It should work if remove the s.
    Rabii
    Web Dev

    Comment

    • dev77
      Senior Member
      • Feb 2018
      • 123

      #3
      Thank you. The hook works GREAT now. I made a change in case anyone ever wants a hook that will write a string to a file. Each time an account record is added or changed and saved, I write a string to a little text file. Each night each of my backup bash scripts scan that file to see if that particular backups should run. The backup script sees "espobak" in there and it will run the script that creates a mysql dump of the production file and loads it into the backup file. Thanks for your help.

      Code:
      <?php
      namespace Espo\Custom\Hooks\Account;
      
      use Espo\ORM\Entity;
      
      class MyHook
      {    
          // An optional parameter, defines in which order hooks will be processed.
          // Lesser value means sooner.
          //public static int $order = 5;
      
          //public function __construct(
              // Define needed dependencies.
          //) {}
      
          public function afterSave(Entity $entity, array $options): void
          {
      
            $myfile = fopen("/usr/home/xxx/bak-scripts/switch-file.txt", "a") or die("Unable to open file!");
      
            $txt = "espobak\n";
            fwrite($myfile, $txt);
           fclose($myfile);
      
      
      
          }
      }
      
      ​
      Last edited by dev77; 03-16-2023, 11:09 PM.

      Comment


      • rabii
        rabii commented
        Editing a comment
        sounds good you can even achieve such logic using Service and custom cron job. but that is just optional
    Working...