【发布时间】:2017-06-27 05:50:33
【问题描述】:
我刚刚阅读了 Symfony 3 官方文档,并指出当我需要从数据库中检索对象时,我应该使用如下内容:
$repository = $em->getRepository('AppBundle:Product');
这里 Product 只是一个没有父级的实体类,所以 Doctrine 通过注解来处理它。但我不确定用引号对模型名称进行硬编码是个好主意。如果稍后我将模型命名为 Good,我应该搜索整个项目并替换 Product on Good。我以 Laravel 为例,每个模型都扩展了基本模型类,所以我可以写:Product::model()->find('nevermind')。 Symfony 3.3 中有这样的选项吗?
【问题讨论】:
-
在此示例中,我没有将产品更改为良好。在每种情况下,您都必须重构每一个部分。
Product::model() -> Good::model()、'AppBundle:Product' -> 'AppBundle:Good'、Product::class -> Good::class不是吗?我认为最接近的是@COil Btw 的答案。学说在这里有不同的方法。模型不应该对数据库或存储库有任何了解。它应该具有尽可能少的依赖关系。好的 ides/plugins 也可以解决这个问题。
标签: php symfony doctrine-orm