php 一个模型中的多个表 - Laravel
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22119118/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Multiple tables in one model - Laravel
提问by Panoply
My Index page uses 3 tables in the database:
我的索引页面使用数据库中的 3 个表:
- index_slider
- index_feature
- footer_boxes
- index_slider
- index_feature
- footer_boxes
I use one controller (IndexController.php) and call the three models like so:
我使用一个控制器 (IndexController.php) 并像这样调用三个模型:
public function index() {
return View::make('index')
->with('index_slider', IndexSlider::all())
->with('index_feature', IndexFeature::all())
->with('footer_boxes', FooterBoxes::all());
}
The three modelsabove need ::all()data, so they are all setup like this:
上面三个模型都需要::all()数据,所以都是这样设置的:
class IndexSlider extends Eloquent {
public $table ='index_slider';
}
note:class name changes for each model
注意:每个模型的类名都会发生变化
Seeing as my index page requires these 3 tables and the fact I am repeating the syntax in each model then should I be using polymorphic relations or setting this up differently? ORM from what I have read should have 1 model for each table, but I can't help but feel this would be silly in my situation and many others. DRY (don't repeat yourself) looses meaning in a sense.
看到我的索引页需要这 3 个表,而且我在每个模型中重复语法的事实,那么我应该使用多态关系还是以不同的方式设置它?从我读到的 ORM 应该为每个表有 1 个模型,但我不禁觉得这在我和许多其他情况下会很愚蠢。DRY(不要重复自己)在某种意义上失去了意义。
What would be the best approach to take here or am I on the right track?
在这里采取的最佳方法是什么,或者我是否在正确的轨道上?
回答by Chris Goosey
Firstly I should say each model is written for a specific table, you can't squeeze three tables into one model unless they are related. See Here
首先我应该说每个模型都是为一个特定的表编写的,除非它们是相关的,否则你不能将三个表压缩到一个模型中。看这里
There are two ways I would go about making your code more DRY. Instead of passing your data in a chain of withs I would pass it as the second parameter in your make:
有两种方法可以让您的代码更加 DRY。我不会将数据以 withs 链的形式传递,而是将其作为 make 中的第二个参数传递:
public function index() {
$data = array(
'index_slider' => IndexSlider::all(),
'index_feature' => IndexFeature::all(),
'footer_boxes' => FooterBoxes::all(),
);
return View::make('index', $data);
}
Passing data as the second parameter. See here
将数据作为第二个参数传递。看这里
The other way I would go about it, and this is a better solution if your application is going to grow large, is to create a service (another model class but not hooked up to eloquent) that when you call will return the necessary data. I would definitely do it this way if you are returning the above data in multiple views.
另一种方法是创建一个服务(另一个模型类,但没有连接到 eloquent),当你调用它时,这是一个更好的解决方案,如果你的应用程序要变大,这是一个更好的解决方案。如果您在多个视图中返回上述数据,我肯定会这样做。
An example of using a service would look something like this:
使用服务的示例如下所示:
<?php
// app/models/services/indexService.php
namespace Services;
use IndexSlider;
use IndexFeature;
use FooterBoxes;
class IndexService
{
public function indexData()
{
$data = array(
'index_slider' => IndexSlider::all(),
'index_feature' => IndexFeature::all(),
'footer_boxes' => FooterBoxes::all(),
);
return $data;
}
}
and your controller:
和您的控制器:
<?php
// app/controllers/IndexController.php
use Services/IndexService;
class IndexController extends BaseController
{
public function index() {
return View::make('index', with(new IndexService())->indexData());
}
}
This service can be expanded with a lot less specific methods and you should definitely change the naming (from IndexService and indexData to more specific class/method names).
这个服务可以用很多不太具体的方法来扩展,你肯定应该改变命名(从 IndexService 和 indexData 到更具体的类/方法名称)。
If you want more information on using Services I wrote a cool article about it here
如果您想了解有关使用服务的更多信息,我在这里写了一篇很酷的文章
Hope this helps!
希望这可以帮助!

