Laravel 4 模型,如何使用它们

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17897373/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 08:11:21  来源:igfitidea点击:

Laravel 4 Models, how to use them

phplaravellaravel-4eloquent

提问by Andreas

I've been looking at Laravel for awhile and I decided to finally pick it up. It's my first time using a PHP framework and I'm having some trouble grasping the purpose of the models.

我已经研究 Laravel 一段时间了,我决定最终选择它。这是我第一次使用 PHP 框架,我在理解模型的目的方面遇到了一些麻烦。

I've been reading a lot of newbie guides and this is pretty much all that is in their model (Laravel wise),

我一直在阅读很多新手指南,这几乎是他们模型中的全部内容(Laravel 明智的),

class Model extends Eloquent {

}

And then in their controller they do something like this,

然后在他们的控制器中他们做这样的事情,

$model = new Model;
$model->text = "text";
$model->save();

I'm no expert at the MVC pattern (probably the biggest newbie out there) but I thought the whole point (or at least a small point) was to separate a lot of actions. And that the model was supposed to be responsible of handling everything DB-wise. So in a way, this seems wrong to me or at least not the best practice.

我不是 MVC 模式的专家(可能是那里最大的新手),但我认为重点(或至少是一小点)是分离很多动作。并且该模型应该负责处理 DB-wise 的所有事情。所以在某种程度上,这对我来说似乎是错误的,或者至少不是最佳实践。

But if you start setting up a bunch of functions you could run into the issue of having one model for every table. Which again, doesn't seem right. So you have to make the model, in a way, ambiguous. In the sense that it can take any actions against any table?

但是,如果您开始设置一堆函数,您可能会遇到为每个表设置一个模型的问题。这又一次,似乎不对。因此,您必须使模型在某种程度上模棱两可。从某种意义上说,它可以对任何表采取任何行动?

It all just seems really confusing to me at the moment.

目前这一切对我来说似乎真的很困惑。

回答by Antonio Carlos Ribeiro

You are going to need a model for evey table, because there are other things related to models that cannot be shared, like column names and validation, but if you think you are repeating yourself, you can create a BaseModel and add all methods or even overload Eloquent methods in it:

您将需要每个表的模型,因为还有其他与模型相关的内容无法共享,例如列名和验证,但如果您认为自己在重复自己,则可以创建一个 BaseModel 并添加所有方法,甚至重载其中的 Eloquent 方法:

class BaseModel extends Eloquent {

    public function whatever() {

    }

    public function save(array $options = []) {
        // do what you need to do
        parent::save();
    }

}

Then create your models using it:

然后使用它创建模型:

class Order extends BaseModel {

}

But you don't need much on a model, if your models and tables names follows Laravel pattern (in this case the table name would be 'orders'), then you just need this simple declaration to have a model working for your table.

但是您对模型不需要太多,如果您的模型和表名称遵循 Laravel 模式(在这种情况下,表名称将是“订单”),那么您只需要这个简单的声明即可让模型为您的表工作。

Edit:

编辑:

Controllers are meant to transfer data from a model to a view, but they should not know too much about your data, almost everything about them should lies in your models ("Fat models, skinny controllers"), so they need to know just enough to have "control".

控制器旨在将数据从模型传输到视图,但他们不应该对您的数据了解太多,关于它们的几乎所有内容都应该在您的模型中(“胖模型,瘦控制器”),因此他们需要知道的就足够了要有“控制”。

class OrdersController extends BaseController {

    public function process()
    {
        $order = Order::find( Input::get('orderId') )->process();

        return View::make('orders.showProcessedOrder')->with('order',$order);
    }

}

回答by Andrew Koper

Models are a wrapper around your database tables. Your database tables are the real-world things you are making your application about. Models allow you to use your programming language for CRUD'ing (creating, reading, updating or deleting) data. OOP is all about classes and objects, and converting things like incoming HTTP requests and data storage into that form.

模型是数据库表的包装器。您的数据库表是您正在制作应用程序的真实世界事物。模型允许您使用编程语言进行 CRUD(创建、读取、更新或删除)数据。OOP 是关于类和对象,并将传入的 HTTP 请求和数据存储等内容转换为该形式。

Your question is a good one. When you learn how to make Web applications, having a three-tiered web app - a presentation layer, a business-logic layer and data-storage layer with data stored in a relational database - works great, and it makes no doggone sense to add an extra layer of database-related stuff in the code.

你的问题很好。当您学习如何制作 Web 应用程序时,拥有一个三层 Web 应用程序——一个表示层、一个业务逻辑层和一个数据存储层,其数据存储在关系数据库中——效果很好,并且添加没有任何意义代码中与数据库相关的额外层。

And, like Antonio wrote, in MVC programming, "fat models, skinny controllers" is what you're working towards. The controller ideally should just be a couple of lines of code that pass the incoming request to the correct model where it could be validated, added to the database, etc. (But it is easiest to put that in the controller while you're first learning/figuring MVC out.)

而且,就像 Antonio 所写的,在 MVC 编程中,“胖模型,瘦控制器”是您正在努力的方向。理想情况下,控制器应该只是几行代码,将传入的请求传递给正确的模型,在那里可以对其进行验证、添加到数据库等。(但最简单的方法是在您第一次使用时将其放入控制器中学习/弄清楚 MVC。)