Announcement

Collapse
No announcement yet.

Access entityManager in Controller?

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

  • 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 $requestResponse $response): ?stdClass
    {
    $this->entityManager //deprecated ?
    }



  • #2
    Hi,

    The answer is not to extend controllers but use an Action Class. Or a custom controller.

    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.

  • #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...
    X