在 Symfony 2 的监听器中访问数据库

发布于 2024-12-26 03:14:16 字数 875 浏览 3 评论 0原文

我们需要在侦听器中访问数据库信息。 我们在service.yml中配置监听器 听众就像是:

namespace company\MyBundle\Listener;

use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class RequestListener
{
    protected $container;

public function __construct(ContainerInterface $container)
{
    $this->container = $container;
}

public function onKernelRequest(GetResponseEvent $event)
{
...

我们如何才能在 onKernelRequest 函数中访问主义?

我尝试从控制器扩展并执行以下操作:

        $em = $this->getDoctrine()->getEntityManager(); 

它有效,但我认为这是一个不好的做法。

We need to access to database info in a listener.
We configure the listener in a service.yml
The listener is like:

namespace company\MyBundle\Listener;

use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class RequestListener
{
    protected $container;

public function __construct(ContainerInterface $container)
{
    $this->container = $container;
}

public function onKernelRequest(GetResponseEvent $event)
{
...

How can we access to doctrine at onKernelRequest function?

I tried to extends from controller and do:

        $em = $this->getDoctrine()->getEntityManager(); 

and it works but I think this is a bad practice.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

超可爱的懒熊 2025-01-02 03:14:16

您只需注入服务容器即可。首先更改构造函数以获取 EntityManager:

use Doctrine\ORM\EntityManager;

class RequestListener {
    protected $em;
    function __construct(EntityManager $em)
    {
        $this->em = $em;
    }
    //...
}

接下来配置您的服务:

#...
services:
    foo.requestlistener:
        class: %foo.requestlistener.class%
        arguments:
            - @doctrine.orm.entity_manager

You can just inject the service container. First change the constructor to get an EntityManager:

use Doctrine\ORM\EntityManager;

class RequestListener {
    protected $em;
    function __construct(EntityManager $em)
    {
        $this->em = $em;
    }
    //...
}

And next configure your service:

#...
services:
    foo.requestlistener:
        class: %foo.requestlistener.class%
        arguments:
            - @doctrine.orm.entity_manager
单身狗的梦 2025-01-02 03:14:16

看起来您正在将服务容器注入侦听器,因此您可以通过以下方式访问 Doctrine:

$doctrine = $this->container->get('doctrine');

It seems like you're injecting the service container into the listener, so you can access Doctrine this way:

$doctrine = $this->container->get('doctrine');
☆獨立☆ 2025-01-02 03:14:16

如果您的用例允许您直接使用 Doctrine 事件监听器,

#services.yml
qis.listener.contractBundleStatusListener:
    class: Acme\AppBundle\EventListener\MyListener
    tags:
        - { name: doctrine.event_listener, event: postPersist }

您可以从 LifecycleEventArgs 获取实体管理器:

<?php

use Doctrine\ORM\Event\LifecycleEventArgs;

class MyListener
{
    public function postPersist(LifecycleEventArgs $args)
    {
        $entity = $args->getEntity();

        if ($entity instanceof Foo) {
            $entityManager = $args->getEntityManager();

            $entityManager->persist($entity);
            $entityManager->flush();
        }
    }
}

If your use case allows you to use a Doctrine Event Listener directely

#services.yml
qis.listener.contractBundleStatusListener:
    class: Acme\AppBundle\EventListener\MyListener
    tags:
        - { name: doctrine.event_listener, event: postPersist }

you can get the Entity Manager from the LifecycleEventArgs:

<?php

use Doctrine\ORM\Event\LifecycleEventArgs;

class MyListener
{
    public function postPersist(LifecycleEventArgs $args)
    {
        $entity = $args->getEntity();

        if ($entity instanceof Foo) {
            $entityManager = $args->getEntityManager();

            $entityManager->persist($entity);
            $entityManager->flush();
        }
    }
}
琉璃繁缕 2025-01-02 03:14:16

我仍然是 Symfony 的新手,但是您是否尝试过将 doctrine 服务传递给您的侦听器而不是服务容器?

或者,您已经传递了服务容器,因此它应该像调用
$this->container->get('doctrine') 一样简单。另外,前段时间我在 IRC 中被告知,传递服务容器通常被认为是不好的做法。最好传递您需要的个性化服务。

I'm kind of a novice at Symfony still, but have you tried passing the doctrine service to your listener instead of the service container?

Alternately, you are already passing the service container, so it should be as simple as calling
$this->container->get('doctrine'). Also, I was told in IRC some time ago that passing the service container is usually considered bad practice. It's better to pass the individual services that you need.

忆梦 2025-01-02 03:14:16

我不会将业务逻辑交给侦听器,因为它们仅用于侦听事件。以及如何使用学说为侦听器编写测试...

我会将访问内容的学说放入不同的类,然后在侦听器中调用它。

I would not put business logic to listeners as the are only for listening to events. And how would you write tests for the listener using doctrine ...

I'd put the doctrine accessing stuff to a different class and then call it in the listener.

雨落□心尘 2025-01-02 03:14:16

在 symfony 4 中你应该像这样使用依赖注入

class eventSubscriber implements EventSubscriberInterface
{
    /**
     * @var EntityManagerInterface
     */
    private $em;


 public function __construct(EntityManagerInterface $em)
    {

        $this->em = $em;
    }
}

in symfony 4 you should use dependency injection like this

class eventSubscriber implements EventSubscriberInterface
{
    /**
     * @var EntityManagerInterface
     */
    private $em;


 public function __construct(EntityManagerInterface $em)
    {

        $this->em = $em;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文