|
| 1 | +## Eloquent ORM中的方法find方法的实现流程 |
| 2 | + |
| 3 | +## Eloquent ORM是什么? |
| 4 | + |
| 5 | +Laravel 的 Eloquent ORM 提供了漂亮、简洁的 ActiveRecord 实现来和数据库交互。每个数据库表都有一个对应的「模型」用来与该表交互。你可以通过模型查询数据表中的数据,并将新记录添加到数据表中。 |
| 6 | + |
| 7 | + |
| 8 | +## 简单例子 |
| 9 | + |
| 10 | +```php |
| 11 | + |
| 12 | +namespace App; |
| 13 | + |
| 14 | +use Illuminate\Database\Eloquent\Model; |
| 15 | + |
| 16 | +class Article extends Model |
| 17 | +{ |
| 18 | + // |
| 19 | +} |
| 20 | + |
| 21 | +Article::find(1); |
| 22 | + |
| 23 | +``` |
| 24 | + |
| 25 | +## 这种是怎么实现的呢?我们来简单模拟下。跟实际上要差好多。 |
| 26 | + |
| 27 | + |
| 28 | +```php |
| 29 | + |
| 30 | +class Model{ |
| 31 | + |
| 32 | + // 定义查询所需要的参数 |
| 33 | + protected $wheres; |
| 34 | + protected $limit; |
| 35 | + protected $columns; |
| 36 | + |
| 37 | + // 获取表名,如果没有定义就在第一个字符小写,后面加个s |
| 38 | + public function getTable() |
| 39 | + { |
| 40 | + if (! isset($this->table)) { |
| 41 | + return str_replace( |
| 42 | + '\\', '', Str::snake(Str::plural(class_basename($this))) |
| 43 | + ); |
| 44 | + } |
| 45 | + |
| 46 | + return $this->table; |
| 47 | + } |
| 48 | + |
| 49 | + |
| 50 | + // 根据上面的一些条件拼装sql; |
| 51 | + public function toSql() |
| 52 | + { |
| 53 | + // 这里实现步骤大家可以自己去拼写 |
| 54 | + $sql = ''; |
| 55 | + |
| 56 | + return $sql; |
| 57 | + } |
| 58 | + |
| 59 | + public function get($columns = ['*']) |
| 60 | + { |
| 61 | + $this->columns = $columns; |
| 62 | + |
| 63 | + // 执行mysql语句 |
| 64 | + $results = mysql_query($this->toSql()); |
| 65 | + |
| 66 | + return $results; |
| 67 | + } |
| 68 | + |
| 69 | + // 设置参数 |
| 70 | + public function take($value) |
| 71 | + { |
| 72 | + return $this->limit = 1; |
| 73 | + } |
| 74 | + |
| 75 | + |
| 76 | + public function first($column) |
| 77 | + { |
| 78 | + return $this->take(1)->get($columns); |
| 79 | + } |
| 80 | + |
| 81 | + |
| 82 | + public function where($column, $operator = null, $value = null) |
| 83 | + { |
| 84 | + $this->wheres[] = compact( |
| 85 | + 'type', 'column', 'operator', 'value' |
| 86 | + ); |
| 87 | + |
| 88 | + return $this; |
| 89 | + } |
| 90 | + |
| 91 | + |
| 92 | + public function find($id, $columns = ['*']) |
| 93 | + { |
| 94 | + return $this->where($this->primaryKey, '=', $id)->first($columns); |
| 95 | + } |
| 96 | + |
| 97 | + |
| 98 | + public function __call($method, $parameters) |
| 99 | + { |
| 100 | + return $this->$method(...$parameters); |
| 101 | + } |
| 102 | + |
| 103 | + |
| 104 | + public static function __callStatic($method, $parameters) |
| 105 | + { |
| 106 | + return (new static)->$method(...$parameters); |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | + |
| 111 | +class Article extends Model |
| 112 | +{ |
| 113 | + protected $primaryKey = 'id'; |
| 114 | + |
| 115 | +} |
| 116 | + |
| 117 | +``` |
| 118 | + |
| 119 | +实现步骤 |
| 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 执行 返回结果。 |
| 126 | + |
| 127 | +laravel中封装的比这个要复杂的多,这个只是让大家明白ORM简单的一个find()是怎么编写的 |
| 128 | + |
| 129 | +接下来我们可以去试着使用debug,来查看关联模型是怎么获取数据的,比如Article::with('comments')->get(10); |
0 commit comments