【问题标题】:Injecting @session into EntityRepository将@session 注入 EntityRepository
【发布时间】:2013-12-21 16:40:33
【问题描述】:

我想在我的 Symfony2 项目中覆盖默认的 Doctrine\ORM\EntityRepository 类,以便我可以访问 @session 服务,以便我的所有存储库都可以访问某个会话变量(如果已设置)。

经过调查,它似乎没有我希望的那么简单,因为 EntityRepository 是从 Doctrine\ORM\EntityManager 中实例化的,并且这个类是使用静态“create”方法自行实例化的。

我遵循Injecting dependency into entity repository 中的答案,但在实际实现自定义管理器类时遇到了障碍(特别是答案的作者说“但是由于您正在制作自定义实体管理器,您可以将其连接到服务容器并注入您需要的任何依赖项”)。

我已经定义了我的重写 EntityManager 类,具有重写的“create”函数,还重写了“getRepository”函数。正是在这个函数中,我相信我需要将会话添加到存储库中,因为它是在我重写的 EntityRepository 类上使用“setSession”方法创建的,但我不确定如何将会话实际放入管理器中首先,因为 EntityManager 类 (Connection $conn, Configuration $config, EventManager $eventManager) 的其他构造函数参数在 Symfony\Bundle\DoctrineBundle\DependencyInjection\DoctrineExtension "ormLoad" 方法中提供。

我也指定了

doctrine.orm.entity_manager.class: Me\MyBundle\Doctrine\ORM\EntityManager

在我的 config.yml 文件中。

如何让 Symfony 在创建存储库时使用我的自定义 EntityManager 类,并将会话也注入其中?

【问题讨论】:

    标签: symfony doctrine


    【解决方案1】:

    Florian,here,解释了如何通过服务创建存储库:

    my_service:
        class: Doctrine\Common\Persistence\ObjectRepository
        factory_service: doctrine # this is an instance of Registry
        factory_method: getRepository
        arguments: [ %mytest.entity% ]
    

    您可以添加 calls 来调用 setSession(作为延迟 DI):

    my_service:
        ...
        calls:
            - [setSession, ["@session"]]
    

    这是你想要做的吗?

    【讨论】:

    • 这就是我想做的事情,但我需要将我的自定义 EntityManager 类设为默认 EntityManager,所以我不知道如何告诉 Doctrine 使用我的而不是他们的跨度>
    【解决方案2】:

    我最终选择了一些稍微不同的东西:

    我用我的自定义类覆盖了doctrine.orm.entity_manager.class 参数,该类简单地扩展了默认类,增加了一个$session 参数(包含getter 和setter),以及覆盖的creategetRepository 函数(以返回实例我的班级而不是默认班级。

    然后我覆盖了EntityRepository 类并实现了一个返回的“getSession”方法

    $this->_em->getSession();
    

    最后,在一个可以访问实体管理器的自定义事件监听器中,我调用了

    $this->entityManager->setSession($session);
    

    这让我可以从每个存储库访问会话。

    【讨论】:

      【解决方案3】:

      在 Symfony 4+ 中,您可以将其设为 ServiceEntityRepository,并且通过自动装配,无需进行任何 services.yaml 更改。

      namespace App\Repository;
      
      use App\Entity\YourEntity;
      use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
      use Doctrine\Common\Persistence\ManagerRegistry;
      use Symfony\Component\HttpFoundation\Session\SessionInterface;
      
      class YourRepository extends ServiceEntityRepository
      {
          private $session;
      
          public function __construct(ManagerRegistry $registry, SessionInterface $session)
          {
              $this->session = $session;
              parent::__construct($registry, YourEntity::class);
          }
      
          public function findSomethingUsingSession()
          {
              $someValue = $this->session->get('some_index');
              // ...
          }
      }
      

      然后在你的控制器中(例如)

      $repository = $this->getDoctrine()->getRepository(YourEntity::class);
      $result = $repository->findSomethingUsingSession();
      

      或者使用依赖注入(推荐)

      public function someAction(YourRepository $repository)
      {
          $result = $repository->findSomethingUsingSession();
          // ...
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-09-04
        • 2019-04-22
        • 2021-09-17
        • 2011-06-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多