【发布时间】:2013-09-22 05:43:10
【问题描述】:
我目前有一种情况,用户可以填写一个表单,这可能会导致创建几个不同的模型。例如,他们发布了一则待售汽车的广告,但在此过程中,他们需要创建新品牌和新车型(因为数据库中尚不存在这些)。
目前,此表单正在“AdvertController”中处理。我现在想将我的数据库交互抽象到一个存储库中。我的问题如下...
- 我应该为数据库交互创建一个存储库,还是每个模型一个存储库?
- 如果每个模型都有一个,哪个文件(控制器或存储库)应处理确定是否需要创建新模型的逻辑。
换句话说,以下哪个工作流是最佳实践(如果确实如此,则两者之一)?
(假设数据库具有以下关系: Advert m-1 CarModel m-1 Brand)
Form completed -> Advert Controller
AdvertController -> tells CarModel repository to create new CarModel model
CarModel_Respository -> creates new CarModel
AdvertController -> sets CarModel relationship with the newly created brand
AdvertController -> tells Brand repository to create new CarModel model
Brand_Respository -> creates new Brand
AdvertController -> tells Advert repository to create new Advert model
Advert_Repository -> creates new Advert
AdvertController -> sets Advert relationship with CarModel
AdvertController -> displays success message
或者……像这样的东西……
Form completed -> AdvertController.
AdvertController -> sends data to DatabaseRepository
DatabaseRepository -> creates new Brand model
DatabaseRepository -> creates new CarModel model
DatabaseRepository -> sets CarModel brand_id as the newly created brand
DatabaseRepository -> creates new Advert model
DatabaseRepository -> sets Advert car_model_id as newly created CarModel
DatabaseRepository -> Sends success message to AdvertController
AdvertController -> sends success message to user.
【问题讨论】:
-
当我们到达控制器时,我们是否已经知道我们需要创建一个新品牌和/或汽车?还是该逻辑需要成为控制器/repo/models/library/其他东西的一部分?
-
是的,该表单提供了确定是否需要创建新模型的必要信息。本质上,用户填写了一个“新车模型”框,为它提供了必要的数据。
-
我猜你不知道控制器的职责是什么。查一下。
标签: php laravel laravel-4 database-abstraction