Announcement

Collapse
No announcement yet.

How do I properly include Di Aware classes for Custom Entity Services

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

  • How do I properly include Di Aware classes for Custom Entity Services

    Hello,

    Consider the following Service.php file. Such files are created in EspoCRM when creating new entities:
    PHP Code:
    <?php

    namespace Espo\Custom\Services;

    class 
    NoticeOfViolation extends \Espo\Core\Templates\Services\Base
    {
    }

    What is the proper syntax for to set and access Di aware things? Suppose I want to access $this->language inside some method of this class. Do I override the parent constructor, if so, what is the proper syntax? Or do I do something this?
    PHP Code:
    <?php

    namespace Espo\Custom\Services;

    use 
    Espo\Core\{
        
    Di,
        
    Templates\Services\Base as BaseService
    };

    /**
    * @internal This class should not be removed as it's used by custom entities.
    * @extends BaseService<\Espo\Custom\Entities\NoticeOfViolation>
    */


    class NoticeOfViolation extends BaseService implements

        
    Di\FileManagerAware,
        
    Di\DataManagerAware,
        
    Di\LanguageAware
    {
        use 
    Di\FileManagerSetter;
        use 
    Di\DataManagerSetter;
        use 
    Di\LanguageSetter;

        
    public function myMethod() {

            
    $this->language->translateOption($value$field,$scope);
        }

    }







  • #2
    I don't recommend to use Record/Service for adding custom methods. Just create any plain PHP class in any directory you like. Pass dependencies via constructor. If you want to call it from a controller class that is inherited from an existing one, then use $this->injectableFactory->create(YourCustomClass::class) to instantiate.


    Regarding Aware interfaces, it's covered in docs. You just add the interface to your class, and trait (that adds the setter method).
    Last edited by yuri; 03-16-2023, 06:31 PM.

    Comment


    • czcpf
      czcpf commented
      Editing a comment
      What our Record/Services used for if not for extending and creating our own methods?
Working...
X