Access entityManager in Controller?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • czcpf
    Senior Member
    • Aug 2022
    • 160

    Access entityManager in Controller?

    Hello,

    I know its not good practice to access entityManager in a controller but I'm wondering what is the suggested way to do so? It seems $this->entityManager is deprecated in Controller in my version of Espo.

    PHP Code:
    namespace Espo\Custom\Controllers;
    
    use Espo\Core\{
    Exceptions\BadRequest,
    Api\Request,
    Api\Response,
    Exceptions\ForbiddenSilent,
    ORM\Entity
    };
    use stdClass;class MyCustomController extends \Espo\Core\Templates\Controllers\Base
    {public function postActionMyCustomAction(Request $request, Response $response): ?stdClass
    {
    $this->entityManager //deprecated ?
    }
    
    } 
    
  • yuri
    Member
    • Mar 2014
    • 8440

    #2
    Hi,

    The answer is not to extend controllers but use an Action Class. Or a custom controller.
    If you find EspoCRM good, we would greatly appreciate if you could give the project a star on GitHub. We believe our work truly deserves more recognition. Thanks.

    Comment


    • czcpf
      czcpf commented
      Editing a comment
      Thanks. I suppose in my case, the action only applies to a custom entity I created. The framework already creates a controller for it so I'm not quite understanding why I would create another custom controller just to access entity-manager.

      I ended up just creating a business class to handle what I needed to do with entityManager and called it in the controller. Thanks.
      Last edited by czcpf; 08-10-2023, 05:49 PM.
  • onepoint0
    Member
    • Jul 2023
    • 41

    #3
    Hi @czcpf​,

    I'm facing the same problem and am inclined to create a custom controller and inject the entity manager into it because it seems the easiest way. But you say above that this is bad practice. Can you please explain why or direct me to somewhere I can read more about it? I don't really know how it should work.

    Thanks,
    Clare

    Comment

    Working...