问题如下:
1)我在数据库中的几个表中有数百万行,所以使用雄辩是没有效率的,因为我也有多个关系。本例中的解决方案是编写自定义DB::raw()、选择和联接,以有效地完成任务。这将返回您可能知道的StdClass。
2)我有4-5个模型,它们的方法相当长,我需要使用这些方法,所以最好的解决方案是为StdClass的每一行创建这些模型的实例,然后使用这些方法。
在OOP模式方面,是否存在将来自StdClass的信息“移植”到模型中的已知“最佳实践”?你们怎么处理这个问题?我愿意接受任何建议,我已经准备好重组代码了。
P.S. Laravel v4.2
发布于 2014-09-04 10:59:16
像这样的东西会对你有用的。只要根据你的需要调整一下:
public function newFromStd(stdClass $std)
{
// backup fillable
$fillable = $this->getFillable();
// set id and other fields you want to be filled
$this->fillable(['id', ... ]);
// fill $this->attributes array
$this->fill((array) $std);
// fill $this->original array
$this->syncOriginal();
$this->exists = true;
// restore fillable
$this->fillable($fillable);
return $this;
}那你就可以做了。
$user = with(new User)->newFromStd( DB::table('users')->first() );
// or make it static if you like:
$user = User::newFromStd( DB::table('users')->first() );https://stackoverflow.com/questions/25660964
复制相似问题