【发布时间】:2015-02-03 14:43:13
【问题描述】:
我正在尝试在我的应用中采用 SOLID 原则。
假设我有这两个模型:
客户(字段=id、name、address 等)有很多:
控股(字段=id、client_id、ticker、holding_date、value)
在我的 ClientsController 中,我可能有这样的方法:
public function show($id)
{
$client = Client::find($id);
$client->setValuations();
$valuations = $client->getValuations();
return View::make('clients.show')-with(compact('client', 'valuations'));
}
因此,在控制器中,我想为客户获取一段时间内的估值。我的客户端模型上的setValuations() 执行了一个相当复杂的查询,该查询汇总了客户端的持有量并将结果集合设置为客户端上的属性。
所以我的客户端模型可能有点像:
class Client extends \Eloquent {
// All the usual model stuff
protected $valuations;
public function setValuations()
{
$this->valuations = DB::table('holdings')
->select('holdings.holding_date', DB::raw('SUM(holdings.value) AS sumofvalue') )
->where('holdings.client_id', $this->id)
->where('holdings.holding_date', DB::raw('LAST_DAY(holdings.holding_date)') )
->groupBy('holdings.holding_date')
->orderBy('holdings.holding_date', 'asc')
->get();
return $this;
}
public function getValuations()
{
return $this->valuations;
}
}
正如我们所看到的,在模型中放入相当多的废话(为了简洁起见,我已经对其进行了很多压缩!)。我应该认为使用存储库模式可能是最好的方法,但我不确定如何构建它。假设我有几个与客户相关的属性需要进行大量查询或处理以确定它们是什么(例如 returns、客户货币交易 - 这需要应用例如,fx 转换为值的集合),如何构建这个的最佳方式以及逻辑应该放在哪里?
【问题讨论】:
标签: php laravel laravel-4 solid-principles