在阅读了其他几个问题之后,似乎不建议让实体类使用存储库。因此,鉴于这些存储库:
class RestaurantRepository {
public function findAll() { ... }
}
class ReviewRepository {
public function findByRestaurant(Restaurant $restaurant) { ... }
}
我不应该在课堂上这样做:
class Restaurant {
public function getReviews() {
// ...
return $restaurantRepository->findByRestaurant($this);
}
}
但是假设我有这个控制器,它为视图提供了一个餐厅列表:
class IndexController {
public function indexAction() {
$restaurants = $restaurantRepository->findAll();
$this->view->restaurants = $restaurants;
}
}
在视图脚本中获取每家餐厅的评论的“良好做法”是什么?因此我不能这样做:
foreach ($this->restaurants as $restaurant) {
$reviews = $restaurant->getReviews();
}
而且我想在视图中注入 ReviewRepository 也不是我们所说的“最佳实践”......
欢迎任何评论!