上篇讲到了数据库Relation的实现,本篇接着讲migrations or database modification logic的功能,此处开始的git是git co aa98553。
本文是orm系列的第三篇,也是Eloquent演化的第二篇,Eloquent系列会尝试着讲清楚Eloquent是如何一步一步演化到目前功能强大的版本的,但是毕竟个人能力有限,不可能分析的非常完善,总会有不懂的地方,所以讲的错误的地方,恳请大牛们能不吝赐教;或者如果有什么地方是没看懂的,也请提出来,因为可能那地方就是我自己没看懂,所以没讲明白,你提出后我们就可以一起讨论,让我们能共同的进步的。
数据库蓝图
先开始本文的主题。数据库管理相关的代码都放在Schema目录下, 最开始的结构如下:
src/Illuminate/Database/Schema
├── Blueprint.php
└── Builder.php
就两个文件Blueprint和Builder,Schema/Builder负责提供数据库操作的面向对象似的操作,而Schema/Blueprint则负责存储具体的操作数据,包括数据库操作的命令和数据库表的定义,因此有下面的结构:

接着,我们看看是怎么使用Blueprint的,下看创建table
$grammar = new Illuminate\Database\Schema\Grammars\MySqlGrammar;
$blueprint = new Blueprint('users');
$blueprint->create();
$blueprint->increments('id');
$blueprint->string('email');
$statements = $blueprint->toSql($grammar);
此时返回的$statements是一个array,有count($statements)==1,并且$statements[0]为:
create table `users` (`id` int not null auto_increment primary key, `email` varchar(255) not null)
我们现在来看下上述是怎么实现的?
首先构造函数传入表名users,而create则是加了一个命令
public function create()
{
return $this->addCommand('create');
}
addCommand则是将命令加入进$this->commands中,而下面的increments函数如下:
public function increments($column)
{
return $this->integer($column, true);
}
public function integer($column, $autoIncrement = false)
{
return $this->addColumn('integer', $column, compact('autoIncrement'));
}
其最终是调用了addColumn将其加入进$this->columns中,下面看下toSql函数,其核心是下面的部分:
foreach ($this->commands as $command)
{
$method = 'compile'.ucfirst($command->name);
if (method_exists($grammar, $method))
{
if ( ! is_null($sql = $grammar->$method($this, $command)))
{
$statements = array_merge($statements, (array) $sql);
}
}
}
对于每个命令,我们都调用grammar的compileCommand函数,此处我们调用的是compileCreate函数,至此我们就分析完了数据库表操作的方法,下面我们来看migrations功能。
数据库迁移
让我们先看目录结构:
src/Illuminate/Database/Console
└── Migrations
├── MigrateCommand.php
├── MigrationRepositoryInterface.php
└── Migrator.php
此处有个新的知识点,也是laravel中一大亮点Artisan,Artisan是 Laravel 自带的命令行接口名称,此处不做具体的介绍了,有机会再细说的,当我们在命令行中执行php artisan command的时候,会去调用migrateCommand,然后最后会调用Migrator中的函数runMigrations函数,看下面分析:
public function runMigrations(OuputInterface $output, $package, $path, $pretend = false)
{
$this->output->writeln('Running migrations at path: '.$path.'');
// 从文件中获取migrate files
$files = $this->getMigrationFiles($path);
// 获取已经执行的migration
$ran = $this->repository->getRanMigrations($package);
// 比较不同
$migrations = array_diff($files, $ran);
// 执行未执行的migration
$this->runMigrationList($output, $migrations, $package, $pretend);
}
大版本前夜
看完上面的最基本版本的migrator,我们跨越下直接来看tag v1.1.1版本的eloquent,
git co v1.1.1
此版本是v4.0.0之前的一个版本,从这以后laravel会以组件的形式组织各个功能,让我们分析下v1.1.1的版本,目前具有哪些功能,都是怎么实现的,先看下目录结构:

分别介绍下:
Console和Migrations:这是本篇讲migrations or database modification logic的功能
Eloquent:是前一篇讲的对于Active Record模式中Model的功能,包括了Model、Builder和Relation功能,忘记的可以去看前一篇orm 系列 之 Eloquent演化历程1的内容
Query:包含了最基本的Sql的操作和语法逻辑,类似于自定义了一个DSL语言,提供了面向对象的操作方式
Schema:这也是本篇讲migrations or database modification logic的功能,主要是对数据库表操作sql的建模
此处Connectors是之前没有介绍过的,Connectors是在f917efa中第一次加入的,我们看下到底做了什么,其目录结构是:
src/Illuminate/Database/Connectors
├── ConnectionFactory.php
├── Connector.php
├── ConnectorInterface.php
├── MySqlConnector.php
├── PostgresConnector.php
├── SQLiteConnector.php
└── SqlServerConnector.php
看名字我们就知道大致是怎么回事了,ConnectorInterface定义了接口,Connector是基类,ConnectionFactory是工厂模式,负责创建具体的Connectors,再看下Connector文件,里面有个函数叫:
public function createConnection($dsn, array $config, array $options)
{
$username = array_get($config, 'username');
$password = array_get($config, 'password');
return new PDO($dsn, $username, $password, $options);
}
其实就是对创建PDO的封装,随着代码复杂度的提高,我们根据SOLID原则(SOLID原则可以看The Clean Architecture in PHP 读书笔记(三)),将创建PDO这部分功能单独抽离出来,变为了Connectors,然后根据SOLID原则,我们再继续看下ConnectionInterface和ConnectionResolverInterface,分别负责的功能可以看下图:


我们可以看到ConnectionInterface负责数据库的操作,ConnectionResolverInterface负责connection的管理,原先这些功能在稍早的版本中都是揉在一起的,还是那个观点:
随着项目复杂度的提升,我们遵循关注点分离的原则,不断去对系统做解耦工作
新增功能
我们接着本篇开头介绍的migrate功能,来看下v1.1.0版本中有的功能,
src/Illuminate/Database/Console
├── Migrations
│ ├── BaseCommand.php
│ ├── InstallCommand.php
│ ├── MakeCommand.php
│ ├── MigrateCommand.php
│ ├── RefreshCommand.php
│ ├── ResetCommand.php
│ └── RollbackCommand.php
└── SeedCommand.php
此时Migrations目录下都是支持的命令,而Migrations下面是具体的实现了。
src/Illuminate/Database/Migrations
├── DatabaseMigrationRepository.php
├── Migration.php
├── MigrationCreator.php
├── MigrationRepositoryInterface.php
├── Migrator.php
└── stubs
├── blank.php
├── create.php
└── update.php
我们可以看到Eloquent中到处都是接口,接口定义了要满足的契约,彼此之间都过接口交流,最大的进行了解耦。
我们通过一个比较有意思的命令Make来看下migration的实现,make的作用是新建一个migration文件,其会根据命令函数参数,去读取src/Illuminate/Database/Migrations/stubs下面的3个文件中的一个,然后调用下面的函数
protected function populateStub($name, $stub, $table)
{
$stub = str_replace('{{class}}', camel_case($name), $stub);
// Here we will replace the table place-holders with the table specified by
// the developer. This is useful for quickly creaeting a tables creation
// or update migration from the console instead of typing it manually
if ( ! is_null($table))
{
$stub = str_replace('{{table}}', $table, $stub);
}
return $stub;
}
做一个简单的字符串替换,然后产生文件,然后还有一个比较有意思的是DatabaseMigrationRepository类,其作用是:我们需要记录migration哪些已经做了,哪些还没有做,这些记录方式我们通过DatabaseMigrationRepository来实现,最终是通过将执行记录以log的形式插入到数据库中。
本文最后讲下Eloquent中新增的对象之间的关系:多态关系,以下内容摘自[ Laravel 5.3 文档 ] Eloquent ORM —— 关联关系
表结构
多态关联允许一个模型在单个关联下属于多个不同模型。例如,假设应用用户既可以对文章进行评论也可以对视频进行评论,使用多态关联,你可以在这两种场景下使用单个comments表,首先,让我们看看构建这种关联关系需要的表结构:
posts
id - integer
title - string
body - text
videos
id - integer
title - string
url - string
comments
id - integer
body - text
commentable_id - integer
commentable_type - string
两个重要的需要注意的列是 comments 表上的 commentable_id 和 commentable_type。commentable_id列对应 Post 或Video 的 ID 值,而 commentable_type 列对应所属模型的类名。当访问 commentable 关联时,ORM 根据commentable_type 字段来判断所属模型的类型并返回相应模型实例。
模型结构
接下来,让我们看看构建这种关联关系需要在模型中定义什么:
morphTo();
}
}
class Post extends Model
{
/**
* Get all of the post's comments.
*/
public function comments()
{
return $this->morphMany('App\Comment', 'commentable');
}
}
class Video extends Model
{
/**
* Get all of the video's comments.
*/
public function comments()
{
return $this->morphMany('App\Comment', 'commentable');
}
}
以上内容来自[ Laravel 5.3 文档 ] Eloquent ORM —— 关联关系,接下去让我们看下这是怎么实现的?
首先是morphMany的构造函数:
public function __construct(Builder $query, Model $parent, $morphName)
{
$this->morphType = "{$morphName}_type";
$this->morphClass = get_class($parent);
parent::__construct($query, $parent, "{$morphName}_id");
}
然后morphMany其实是扩展是HasOneOrMany,然后再加限制的时候,新增了一个$this->query->where($this->morphType, $this->morphClass);,通过这个限制就可以解决多态关联了,那上面的例子来说,就是多个条件commentable_type=Video,至于HasOneOrMany的分析参考上一篇文章。
总结
以上就是v4.0.0之前的Eloquent的大致功能,目前orm功能已经完善了,数据库迁移功能,Active Record模式的实现,下一步Eloquent的方向是什么呢?让我们跟着git继续追踪吧^_^
参考
[ Laravel 5.3 文档 ] Eloquent ORM —— 关联关系
这是orm的第三篇,你的鼓励是我继续写下去的动力,期待我们共同进步。
