如何从实体获取存储库?

发布于 2024-12-05 15:21:48 字数 540 浏览 1 评论 0原文

我有一个名为 Game 的实体,以及一个名为 GameRepository 的相关存储库:

/**
 * @ORM\Entity(repositoryClass="...\GameRepository")
 * @ORM\HasLifecycleCallbacks()
 */
class Game {
    /**
     * @ORM\prePersist
     */
    public function setSlugValue() {
        $this->slug = $repo->createUniqueSlugForGame();
    }
}

在 prePersist 方法中,我需要确保 Game 的 slug 字段是唯一的,这需要数据库查询。为了进行查询,我需要访问 EntityManager。我可以从 GameRepository 内部获取 EntityManager。那么:如何从游戏中获取 GameRespository?

I have an Entity called Game with a related Repository called GameRepository:

/**
 * @ORM\Entity(repositoryClass="...\GameRepository")
 * @ORM\HasLifecycleCallbacks()
 */
class Game {
    /**
     * @ORM\prePersist
     */
    public function setSlugValue() {
        $this->slug = $repo->createUniqueSlugForGame();
    }
}

In the prePersist method, I need to ensure that the Game's slug field is unique, which requires a database query. To do the query, I need access to the EntityManager. I can get the EntityManager from inside GameRepository. So: how do I get the GameRespository from a Game?

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

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

发布评论

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

评论(4

泛泛之交 2024-12-12 15:21:48

实际上,您可以在实体中获取存储库,并且只能在生命周期回调期间获取。您已经非常接近它了,您所要做的就是接收 LifecycleEventArgs 参数。

另请参阅 http://docs.doctrine- project.org/projects/doctrine-orm/en/latest/reference/events.html

use Doctrine\ORM\Event\PrePersistEventArgs;

/**
 * @ORM\Entity(repositoryClass="...\GameRepository")
 * @ORM\HasLifecycleCallbacks()
 */
class Game {
    /**
     * @ORM\prePersist
     */
    public function setSlugValue( PrePersistEventArgs $event ) {
        $entityManager = $event->getEntityManager();
        $repository    = $entityManager->getRepository( get_class($this) );
        
        $this->slug = $repository->createUniqueSlugForGame();
    }
}

PS。我知道这是一个老问题,但我回答它是为了帮助任何未来的谷歌用户。

You actually can get the repository in your entity and only during a lifecycle callback. You are very close to it, all you have to do is to receive the LifecycleEventArgs parameter.

Also see http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/events.html

use Doctrine\ORM\Event\PrePersistEventArgs;

/**
 * @ORM\Entity(repositoryClass="...\GameRepository")
 * @ORM\HasLifecycleCallbacks()
 */
class Game {
    /**
     * @ORM\prePersist
     */
    public function setSlugValue( PrePersistEventArgs $event ) {
        $entityManager = $event->getEntityManager();
        $repository    = $entityManager->getRepository( get_class($this) );
        
        $this->slug = $repository->createUniqueSlugForGame();
    }
}

PS. I know this is an old question, but I answered it to help any future googlers.

赤濁 2024-12-12 15:21:48

你不知道。原则 2 中的实体应该知道实体管理器或存储库。

针对您提出的情况的典型解决方案是向存储库(或服务类)添加一个方法,该方法用于创建(或调用以存储)新实例,并且还会生成唯一的 slug 值。

You don't. Entities in Doctrine 2 are supposed to not know of the entity manager or the repository.

A typical solution to the case you present would be to add a method to the repository (or a service class) which is used to create (or called to store) new instances, and also produces a unique slug value.

迷雾森÷林ヴ 2024-12-12 15:21:48

您可以在您的实体中注入学说实体管理器
(使用 JMSDiExtraBundle)
并拥有这样的存储库:

/**
 * @InjectParams({
 *     "em" = @Inject("doctrine.orm.entity_manager")
 * })
 */
    public function setInitialStatus(\Doctrine\ORM\EntityManager $em) {


    $obj = $em->getRepository('AcmeSampleBundle:User')->functionInRepository();
    //...
}

请参阅: http://jmsyst.com/bundles/JMSDiExtraBundle/1.1 /注释

you can inject the doctrine entity manager in your entity
(using JMSDiExtraBundle)
and have the repository like this:

/**
 * @InjectParams({
 *     "em" = @Inject("doctrine.orm.entity_manager")
 * })
 */
    public function setInitialStatus(\Doctrine\ORM\EntityManager $em) {


    $obj = $em->getRepository('AcmeSampleBundle:User')->functionInRepository();
    //...
}

see this : http://jmsyst.com/bundles/JMSDiExtraBundle/1.1/annotations

风尘浪孓 2024-12-12 15:21:48

为了保持逻辑封装,而不必更改保存实体的方式,您需要考虑使用更强大的 Doctrine 事件,而不是简单的 prePersist 生命周期事件,这些事件可以访问的不仅仅是实体本身。

您可能应该查看 DoctrineSluggableBundleStofDoctrineExtensionsBundle 捆绑包可能会这样做正是您所需要的。

In order to keep the logic encapsulated without having to change the way you save the entity, instead of the simple prePersist lifecycle event you will need to look at using the more powerful Doctrine events which can get access to more than just the entity itself.

You should probably look at the DoctrineSluggableBundle or StofDoctrineExtensionsBundle bundles which might do just what you need.

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