Calling a service method from a job class. [ESPO Upgrade 6.1.0 -> 7.0.1]

Collapse
X
 
  • Time
  • Show
Clear All
new posts

  • telecastg
    commented on 's reply
    Got it, thanks so much for the explanation.

  • yuri
    commented on 's reply
    When one wants to write some business logic, it's not needed to create/extend a service class as it was before v6. ServiceFactory is deprecated. Create just a plain PHP class in any namespace you like (Espo\Custom\*, Espo\Modules\ModuleName\*), inject all that is needed via constructor (EntityManager, Config, Metadata, etc.).

    Then your Job, Hook etc. can use this class. Inject this class via constructor.
    Last edited by yuri; 04-21-2022, 05:45 PM.

  • yuri
    commented on 's reply
    I recommend to read and understand the article about dependency injection in the documentation. Almost every class is created via DI factory. The principle is the same for all them.

    There's NO such thing as SERVICES in Espo anymore. There is the record service class (Espo\Core\Record\Service) that handles API requests. It's a layer between CRUD controller and internal logics (ORM, Select framework). It's NOT RECOMMENDED to extend the Record\Service class. There should not be a need to extend it as almost any customization can be done with different extension frameworks (hooks, duplicate checkers etc). Even though there are some classes extended from the Record\Services, it's because it's a legacy and I didn't bother to remove/refactor them yet.

    This record service class does have dependency recordServiceContainer. It's already there. You can include this class Espo\Core\Record\ServiceContainer to any class (Job, Hook, you name it) and use it the same way.

  • telecastg
    commented on 's reply
    This looks different to me than the solution presented for the question of calling a Service class from another Service class. I will include a link to this example as well in my posting.

    Would you recommend this approach as the preferred way to substitute the use of the deprecated ServiceFactory class ?

    Thanks
    Last edited by telecastg; 04-20-2022, 09:03 PM.

  • yuri
    replied
    Example: https://github.com/espocrm/espocrm/b...Emails.php#L43

    Inject any class you want into the constructor.

    Leave a comment:


  • yuri
    replied
    This is the same as https://forum.espocrm.com/forum/deve...e-6-1-0-7-0-10. No need to create a topic for each class.

    Jobs, Controlles, etc.. they all are same. Inject needed classes into a constructor. It's not Espo-specific magic. It's a common principle of programming of last 2 decades.

    Leave a comment:


  • Calling a service method from a job class. [ESPO Upgrade 6.1.0 -> 7.0.1]

    Hello

    In ESPO 6.1.0 I had:
    Code:
    protected function getRecordService($name)
    {[INDENT]if ($this->getServiceFactory()->checkExists($name)) {[/INDENT][INDENT=2]$service = $this->getServiceFactory()->create($name);[/INDENT][INDENT]} else {[/INDENT][INDENT=2]$service = $this->getServiceFactory()->create('Record');
    $service->setEntityType($name);[/INDENT][INDENT]}
    return $service;[/INDENT]
     }
    
    public function run()
    {[INDENT]$this->getRecordService('ServiceName')->serviceFuntionName($data);[/INDENT]
     }
    This would call the `serviceFunctionName` function which is under the Services/ class.

    How do I achieve the same in 7.0.1 as I cannot use the getServiceFactory() method from job class anymore.

    telecastg Any ideas? Thank You :-)
Working...