Hook dependencies

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • alasdaircr
    Active Community Member
    • Aug 2014
    • 525

    Hook dependencies

    Hello I have the following code:
    PHP Code:
    <?php
    
    namespace Espo\Modules\MYMODULE\Hooks\Common;
    
    use Espo\ORM\Entity;
    
    class MYCLASS extends \Espo\Core\Hooks\Base
    {
        protected function init()
        {
            parent::init();
            $this->addDependency('serviceFactory');
        }
    
        protected function getServiceFactory()
        {
            return $this->getInjection('serviceFactory');
        }
    
        public function beforeSave(Entity $entity, $initialSync = FALSE)
        {
            $service = $this->getServiceFactory()->create('MyServiceName');
            ^^^
        }
    }
    I'm seeing an error message in the beforeSave call:

    E_NOTICE: Undefined index: serviceFactory {"code":8,"message":"Undefined index: serviceFactory","file":"/var/espo/espocrm/application/Espo/Core/Hooks/Base.php","line":77,"context":{"name":"serviceFact ory"}} []

    I've rebuilt, checked for typos etc. Why wouldn't the addDependency call be working for me?
  • tanya
    Senior Member
    • Jun 2014
    • 4308

    #2
    Hello
    Maybe hook runs before serviceFactory is loaded. It's a global hook, it could be called too early.
    Try to limit entity list it's used for.
    You could also try $this->getInjection('container')->get('serviceFactory');
    But I'm not sure.

    Comment

    • alasdaircr
      Active Community Member
      • Aug 2014
      • 525

      #3
      Thanks tanya I went with a different approach,

      Comment

      Working...