Skip to content

Commit 469d282

Browse files
committed
docs: Eloquent ORM 中的方法 find 方法的实现流程
1 parent e477cdb commit 469d282

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

laravel/readme/30. 2019-07-27-Eloquent ORM中的方法find方法的实现流程.md

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
## Eloquent ORM中的方法find方法的实现流程
1+
## Eloquent ORM 中的方法 find 方法的实现流程
22

3-
## Eloquent ORM是什么
3+
## Eloquent ORM 是什么
44

55
Laravel 的 Eloquent ORM 提供了漂亮、简洁的 ActiveRecord 实现来和数据库交互。每个数据库表都有一个对应的「模型」用来与该表交互。你可以通过模型查询数据表中的数据,并将新记录添加到数据表中。
66

@@ -34,7 +34,7 @@ class Model{
3434
protected $limit;
3535
protected $columns;
3636

37-
// 获取表名,如果没有定义就在第一个字符小写,后面加个s
37+
// 获取表名, 如果没有定义就在第一个字符小写,后面加个 s
3838
public function getTable()
3939
{
4040
if (! isset($this->table)) {
@@ -47,7 +47,7 @@ class Model{
4747
}
4848

4949

50-
// 根据上面的一些条件拼装sql;
50+
// 根据上面的一些条件拼装 sql;
5151
public function toSql()
5252
{
5353
// 这里实现步骤大家可以自己去拼写
@@ -60,7 +60,7 @@ class Model{
6060
{
6161
$this->columns = $columns;
6262

63-
// 执行mysql语句
63+
// 执行 mysql 语句
6464
$results = mysql_query($this->toSql());
6565

6666
return $results;
@@ -117,13 +117,13 @@ class Article extends Model
117117
```
118118

119119
实现步骤
120-
1. Article::find(1); 发现没有find方法就回去掉Model的__callStatic
121-
2. __callStatic方法又回去调用__call方法,这时发现有find方法
122-
3. find方法会调用where拼装要查询的参数,然后调用first()
123-
4. 因为first() 只需要取1条,所以设置$limit 1
124-
5. 最后组装sql
125-
6. 交给mysql 执行 返回结果。
120+
1. Article::find(1); 发现没有 find 方法就回去掉 Model 的__callStatic
121+
2. __callStatic 方法又回去调用__call 方法,这时发现有 find 方法
122+
3. find 方法会调用 where 拼装要查询的参数,然后调用 first()
123+
4. 因为 first() 只需要取 1 条,所以设置 $limit 1
124+
5. 最后组装 sql
125+
6. 交给 mysql 执行 返回结果。
126126

127-
laravel中封装的比这个要复杂的多,这个只是让大家明白ORM简单的一个find()是怎么编写的
127+
laravel 中封装的比这个要复杂的多,这个只是让大家明白 ORM 简单的一个 find() 是怎么编写的
128128

129-
接下来我们可以去试着使用debug,来查看关联模型是怎么获取数据的,比如Article::with('comments')->get(10);
129+
接下来我们可以去试着使用 debug, 来查看关联模型是怎么获取数据的,比如 Article::with('comments')->get(10);

0 commit comments

Comments
 (0)