使用 Doctrine ORM:自定义存储库
我有大约 10 个实体: \App\Entity\User、\App\Entity\ Group,...
每个实体都有自己的存储库:\App\Repository\UserRepository,...在十几个方法中的每一个中。
在 Doctrine 中的访问方法:
$userRepository = $em->getRepository('App\Entity\User');
但是,这并不方便,因为丢失了代码完成。
问题:如何在不增加静态连接代码的情况下组织存储库的工作? 我应该使用静态方法 get 吗?
class UserRepository extends EntityRepository
{
/**
* @static
* @return \App\Repository\UserRepository
*/
public static function get ()
{
$em = \Registry::getInstance()->get('em');
return $em->getRepository('App\Entity\User');
}
}
我看到了与交响乐教义一起工作的实现,但是代码完成也存在同样的问题。
I have about 10 entities: \App\Entity\User, \App\Entity\ Group, ...
For each of them has its own repository: \App\Repository\UserRepository, ... In each of a dozen methods.
In Doctrine for access method:
$userRepository = $em->getRepository('App\Entity\User');
However, it is not convenient, because lost code completion.
Question: how to organize work with repositories, without increasing the static connection code?
Should I use a static method get?
class UserRepository extends EntityRepository
{
/**
* @static
* @return \App\Repository\UserRepository
*/
public static function get ()
{
$em = \Registry::getInstance()->get('em');
return $em->getRepository('App\Entity\User');
}
}
I watched as implemented to work with the doctrine of the symphony, but there is the same problem with code completion.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为对此没有任何完全干净的解决方案。
您的选择基本上可以归结为两个:
我真的认为这没什么大不了的。无论如何,存储库通常没有那么多方法,所以记住(或检查)并不难。
但是,您也可以尝试的一件事是:
这应该激活
$repo
的完成大多数 IDE 的。不过,它仍然需要一些额外的打字。I don't think there's any entirely clean solutions to this.
Your options basically boil down to two:
I don't really think it's such a big deal. Repositories don't have that many methods in them usually anyway, so it's not that hard to remember (or check)
However, one thing you could also try is this:
This should activate completion for
$repo
in most IDE's. It still involves some extra typing tho.$this->_em 可从存储库中获取。我一直用它
$this->_em is available from within the repository. I use it all the time