带 getManager 和不带 getManager 的 getRepository 有什么区别?

Luk*_*szy 5 doctrine symfony

我使用了代码:

    $userRepository = $this->getDoctrine()
                ->getManager()
                ->getRepository(User::class);

但今天在分析一些教程时我发现没有 getManager 的代码也有同样的作用

    $userRepository = $this->getDoctrine()
                ->getRepository(User::class);

那么,有什么区别呢?细节决定成败,所以我很好奇。

谢谢,L

小智 5

如果您只有一个连接,情况是一样的: https://symfony.com/doc/current/doctrine.html#persisting-objects-to-the-database

->getManager()
Run Code Online (Sandbox Code Playgroud)

允许您选择连接。

就我个人而言,按照惯例,我总是使用完整格式;)