使用 DBAL 配置的 Symfony 项目中的存储库替换

Repositories reeplacement in a Symfony project configured with DBAL

我在 Symfony 3.0.1 中的一个项目中工作,该项目使用五个数据库并将 DBAL 作为数据访问层。我一直在使用 ORM 的 Symfony 中工作,并且我总是使用下一个 MVC 模型:

数据访问:

CONTROLLER -> REPOSITORY(此处查询)-> ENTITY

显示结果:

CONTROLLER -> render($view,$params) -> VIEW

此模型允许使用简短的控制器,但现在我使用的是 DBAL,所以无法使用存储库。

问题是:

如何使用 DBAL 实现类似的模型?换句话说,我应该把查询放在哪里?

我应该使用服务而不是存储库吗?

注意:我只在该数据库中使用 select 语句。

提前致谢!

您不需要 ORM 即可使用实体。
同样,一旦存储库是设计模式的实现,您就不需要 Doctrine 来构建存储库:

存储库

Mediates between the domain and data mapping layers using a collection-like interface for accessing domain objects. Read more

即使您不能使用 Doctrine ORM,您仍然可以针对抽象模型 class 设计 POPO classes class or/and 模型接口。
您可以通过构造或 setter 方法为每个 model/entity 注入 DBAL Connection 对象。创建回购后 classes 很容易。返回集合对象、保湿项目或使用原始数组由您决定。

编辑#1

我添加了一个我过去(几年前)用过的真实世界的例子,看:

https://github.com/felipsmartins/misc-and-code-snippets/tree/master/php/model-exemple/src/AppBundle/Model

编辑#2

关于服务,如果有专门且定义明确的模型(相对于Anemic Models) working together inside a service which coordinates the whole transaction, in this case it would be what we know by Unit of Work (see Unit of Work Pattern)会更好