本篇文章给大家带来的内容是关于laravel源码解析之model(代码),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

前言

提前预祝猿人们国庆快乐,吃好、喝好、玩好,我会在电视上看着你们。

根据单一责任开发原则来讲,在laravel的开发过程中每个表都应建立一个model对外服务和调用。类似于这样

1

2

3

4

5

6

7

8

namespace app\models;

     

use illuminate\database\eloquent\model;

     

class user extends model

{

    protected $table = 'users';

}

解析

laravel的数据操作分两种

  • db facade

  • eloquent orm

它们除了有各自的特色外,基本的数据操作都是通过 illuminate\database\query\builder 调用方法去完成整个sql。你也可以帮builder这个类作为整个sql操作的基类。这个类涵盖了以下的操作方法(部分展示)

方法
public function select($columns = ['*'])
public function selectsub($query, $as)
public function selectraw($expression, array $bindings = [])
public function fromsub($query, $as)
public function fromraw($expression, $bindings = [])
public function addselect($column)
public function distinct()
public function from($table)
public function join($table, $first, $operator = null, $second = null, $type = 'inner', $where = false)
public function joinwhere($table, $first, $operator, $second, $type = 'inner')
public function joinsub($query, $as, $first, $operator = null, $second = null, $type = 'inner', $where = false)
public function leftjoin($table, $first, $operator = null, $second = null)
public function where($column, $operator = null, $value = null, $boolean = 'and')
public function orwhere($column, $operator = null, $value = null)
public function whereraw($sql, $bindings = [], $boolean = 'and')
public function wherein($column, $values, $boolean = 'and', $not = false)
public function orwherein($column, $values)

可见有很多方法在中国laravel站或者官方文档上都没有体现,所以说就算要精通一款框架,不去看它的源码也是不行的。这个文件在你项目目录中的 vendor/laravel/framework/src/illuminate/database/query 下,你可以自行去查看。

db facade

正常情况下你可能会这样写一个操作

1

db::table('user')->get();

这个操作首先经过laravel的门面指向文件,不过它并不在 app.php 中,而是通过内核直接加载,它在

1

illuminate\foundation\application -> registercorecontaineraliases()

被注册。门面直接调用 illuminate\database\databasemanager 类。

1

2

3

4

5

6

7

8

9

10

11

12

public function registercorecontaineraliases()

{

        foreach ([

            ...

            'encrypter'            => [\illuminate\encryption\encrypter::class, \illuminate\contracts\encryption\encrypter::class],

            'db'                   => [\illuminate\database\databasemanager::class],

            'db.connection'        => [\illuminate\database\connection::class, \illuminate\database\connectioninterface::class],

            'events'               => [\illuminate\events\dispatcher::class, \illuminate\contracts\events\dispatcher::class],

            'files'                => [\illuminate\filesystem\filesystem::class],

            ....

        )

}

illuminate\database\databasemanager 内并没有太多的代码,大多都是处理数据库链接。当你使用 db::table()时,会通过

1

2

3

4

public function __call($method, $parameters)

{

    return $this->connection()->$method(...$parameters);

}

转发,调用的是 illuminate\database\connection ,用户处理 table() 方法,随后会通过 table() 方法指向 illuminate\database\query 类,开头我们讲过这个类了,这里就不多说了,随后就是各种sql的拼接->执行sql->结束战斗

eloquent orm

eloquent orm 与db facade 类似,首先每个 eloquent orm 都需要继承父类 illuminate\database\eloquent\model 
你大概会这样写

1

user::find(1)

父类是不存在这个方法的,它会通过

1

2

3

4

public static function __callstatic($method, $parameters)

{

    return (new static)->$method(...$parameters);

}

去转发请求调用。同理

1

user::get()

则是通过

1

2

3

4

5

6

7

8

public function __call($method, $parameters)

{

    if (in_array($method, ['increment', 'decrement'])) {

        return $this->$method(...$parameters);

    }

         

    return $this->newquery()->$method(...$parameters);

}

去调用,这个方法最终以 new builder() 而告终,

1

2

3

4

public function neweloquentbuilder($query)

{

    return new builder($query);

}

最后我们到了 illuminate\database\eloquent\builder 文件下,这个类中涵盖了orm的基本操作,例如find , findorfail 等等。如果你在代码用到了get方法,抱歉,这里没有,它依旧会通过__call 方法将你的请求转发到 illuminate\database\query\builder 类中

1

$this->query->{$method}(...$parameters);

至此就完成了整个数据操作。

以上就是laravel源码解析之model(代码)的详细内容