在服务中注入Doctrine Entity Manager - 不好的做法?

 暴躁的玩具 发布于 2023-02-09 11:22

使用https://insight.sensiolabs.com扫描/检查我的代码,我收到以下警告:

The Doctrine Entity Manager should not be passed as an argument.

为什么在服务中注入实体管理器是一种不好的做法?什么是解决方案?

1 个回答
  • 关于存储库不能持久化实体的评论.

    class MyRepository extends EntityRepository
    {
        public function persist($entity) { return $this->_em->persist($entity); }
        public function flush  ()        { return $this->_em->flush  (); }
    

    我喜欢让我的存储库或多或少地遵循"标准"存储库接口.所以我这样做:

    interface NyRepositoryInterface
    [
        function save($entity);
        function commit();
    }
    class MyRepository extends EntityRepository implements MyRepositoryInterface
    {
        public function save  ($entity) { return $this->_em->persist($entity); }
        public function commit()        { return $this->_em->flush  (); }
    

    这允许我定义和注入非教义存储库.

    您可能反对必须将这些辅助函数添加到每个存储库.但我发现有点复制/粘贴是值得的.特征也可能对此有所帮助.

    这个想法是脱离实体经理的整个概念.

    2023-02-09 11:24 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有