laravel 什么是 ORM?雄辩的 ORM?

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

What is an ORM? Eloquent ORM?

phpooplaravelorm

提问by Seojudo

I'm a new to the OOP programming . But I think I understand what is an ORM: in practice, the ORM libraries allow to abstract the database in an object and handle it as if it were a class in OOP programming.

我是 OOP 编程的新手。但我想我理解什么是 ORM:在实践中,ORM 库允许在对象中抽象数据库并处理它,就好像它是 OOP 编程中的一个类。

Am I correct? Eloquent is the library that manages ORM in Laravel and to use that it's necessary extend the model (the business logic of a software in MVC architecture)

我对么?Eloquent 是在 Laravel 中管理 ORM 的库,并且使用它有必要扩展模型(MVC 架构中软件的业务逻辑)

回答by mikedugan

tl;dr: ORMs manage the conversion of simple integer-based relationships in the database to instances of classes that you can work with in your code.

tl;dr:ORM 管理数据库中简单的基于整数的关系到您可以在代码中使用的类的实例的转换。

You're confused in a couple different areas.

你在几个不同的领域感到困惑。

First, and quickly, MVC is one layer of an application; specifically it is part of the presentation layer. Your business logic should be completely independent of the MVC layer.

首先,很快,MVC 是应用程序的一层;具体来说,它是表示层的一部分。您的业​​务逻辑应该完全独立于 MVC 层。

RDBMSs like MySQL and PostgreSQL are inherently bad at establishing and maintaining the various relationships that fundamental to object oriented programming. In the classic blog example, your Postbelongs to a User, and the user might have several posts. If we tried to just recursively serialize these objects and store them, we would end up with a lot of circular (and hence infinite) loops.

像 MySQL 和 PostgreSQL 这样的 RDBMS 本质上不擅长建立和维护面向对象编程的基础的各种关系。在经典博客示例中,您Post属于 a User,并且用户可能有多个帖子。如果我们试图只是递归地序列化这些对象并存储它们,我们最终会得到很多循环(因此是无限)循环。

Instead, we use a system of indices and keys to make a permanent note in the database about some other related object. These might reference a table, but are no representation of the concrete relationship that will be established programmatically.

相反,我们使用索引和键系统在数据库中永久记录一些其他相关对象。这些可能引用一个表,但不代表将以编程方式建立的具体关系。

So then we have the ORM. The ORM knows, via foreign keys and relationship methods in Eloquent, that the user_id column on a row in the posts table actually correlated to a User object. This happens under the hood once you add the relationship (see below) so that you can reference this user at any time, as long as the user exists, without having to work with the database or even be aware that the underlying data isn't already an object.

那么我们就有了 ORM。ORM 通过 Eloquent 中的外键和关系方法知道,posts 表中一行上的 user_id 列实际上与 User 对象相关联。一旦您添加了关系(见下文),这就会发生在幕后,这样您就可以随时引用该用户,只要该用户存在,而无需使用数据库,甚至无需意识到底层数据不是已经是一个对象。

We might see the following in Post.php:

我们可能会看到以下内容Post.php

public function user() {
  return $this->belongsTo(User::class);
}

If we have a user_id of 5, Eloquent uses a little grammatical magic (specifically, lower-snake cases the class and appends _id) to find the user with the id of 5. When you retrieve this, either via eager loading or lazy loading, eloquent will then use the retrieved data to hydrate a Userobject.

如果我们的 user_id 为 5,Eloquent 会使用一些语法魔法(特别是,将类和 _id 附加到小蛇)来查找 id 为 5 的用户。然后 eloquent 将使用检索到的数据来水合一个User对象。

The abstraction lies in the fact that technically the class/object has no knowledge of the database structure, Eloquent instead handles the conversion to and from property names and column names.

抽象在于,从技术上讲,类/对象不了解数据库结构,Eloquent 反而处理属性名和列名之间的转换。

回答by Seojudo

Check out the documentation at laravel.com HERE

此处查看 laravel.com 上的文档