Doctrine repositories are the place where queries are put if you want to share them across your application.
如果要在整个应用程序*享查询,则可以在Doctrine存储库中放置查询。
Is it a good idea to put persistence logic in the repository, so that repositories would be useful for not just querying, but also creating and updating objects?
将持久性逻辑放在存储库中是一个好主意,这样存储库不仅可用于查询,还可用于创建和更新对象吗?
Is there any other place for persistence logic that is not in the controller itself?
持久性逻辑还有其他地方不在控制器本身吗?
1 个解决方案
#1
3
Put it into the service layer. In this case, your controllers know only the service layer, but no repository layer. The service layer can delegate queries to the repository layer or do them by itself — I prefer the latter.
将它放入服务层。在这种情况下,您的控制器只知道服务层,但不知道存储库层。服务层可以将查询委托给存储库层,也可以单独执行 - 我更喜欢后者。
Just a basic example:
只是一个基本的例子:
class CommentService
{
private $em;
public function __construct(EntityManager $em)
{
$this->em = $em;
}
public function find($id)
{
// do a query here or delegate to a repository
}
public function findByPost(Post $post)
{
// do a query here or delegate to a repository
}
public function save(Comment $comment)
{
// exec an operation here
}
public function delete(Comment $comment)
{
// exec an operation here
}
}
#1
3
Put it into the service layer. In this case, your controllers know only the service layer, but no repository layer. The service layer can delegate queries to the repository layer or do them by itself — I prefer the latter.
将它放入服务层。在这种情况下,您的控制器只知道服务层,但不知道存储库层。服务层可以将查询委托给存储库层,也可以单独执行 - 我更喜欢后者。
Just a basic example:
只是一个基本的例子:
class CommentService
{
private $em;
public function __construct(EntityManager $em)
{
$this->em = $em;
}
public function find($id)
{
// do a query here or delegate to a repository
}
public function findByPost(Post $post)
{
// do a query here or delegate to a repository
}
public function save(Comment $comment)
{
// exec an operation here
}
public function delete(Comment $comment)
{
// exec an operation here
}
}