maghead / maghead
快速PHP ORM
Requires
- php: >=5.6
- corneltek/cascading-attribute: ^1.2
- corneltek/cliframework: 4.1.x-dev
- corneltek/codegen: 4.0.x-dev
- corneltek/configkit: ^1.6
- corneltek/serializerkit: ^1.1
- corneltek/validationkit: ^1
- cypresslab/gitelephant: ^1.0
- doctrine/inflector: ^1.0
- maghead/magsql: 4.0.x-dev
- maghead/sqlite-parser: ^4.0
- monolog/monolog: ^1.22
- pimple/pimple: ^3.0
- psr/log: ^1.0
- ramsey/uuid: ^3.5
- symfony/yaml: ^2.8|^3.0|^3.2
- universal/universal: @dev
Requires (Dev)
- ext-apcu: *
- c9s/webaction: 4.0.x-dev
- maghead/sqlite-parser: 4.0.x-dev
- mongodb/mongodb: ^1.1
Suggests
- ext-xarray: *
- mongodb/mongodb: ^1.1
- dev-master / 4.0.x-dev
- 4.0.1
- 4.0.0
- 3.4.x-dev
- 3.4.1
- 3.4.0
- 3.3.2
- 3.3.1
- 3.3.0
- 3.2.0
- 3.1.x-dev
- 3.1.3
- 3.1.2
- 3.1.1
- 3.1.0
- 3.0.x-dev
- 3.0.1
- 3.0.0
- 2.2.4
- 2.2.3
- 2.2.2
- 2.2.1
- 2.2.0
- 2.1.6
- 2.1.5
- 2.1.4
- 2.1.3
- 2.1.2
- 2.1.1
- 2.1.0
- 2.0.2
- 2.0.1
- 2.0.0
- 1.18.x-dev
- 1.18.5
- 1.18.4
- 1.18.3
- 1.18.2
- 1.18.1
- 1.18.0
- 1.17.0
- 1.16.2
- 1.16.1
- 1.16.0
- 1.15.0
- 1.14.8
- 1.14.7
- 1.14.6
- 1.14.5
- 1.14.4
- 1.14.3
- 1.14.2
- 1.14.1
- 1.14.0
- 1.13.5
- 1.13.4
- 1.13.3
- 1.13.2
- 1.13.1
- 1.13.0
- 1.12.6
- 1.12.5
- 1.12.4
- 1.12.3
- 1.12.2
- 1.12.1
- 1.12.0
- 1.11.13
- 1.11.12
- 1.11.11
- 1.11.10
- 1.11.9
- 1.11.8
- 1.11.7
- 1.11.6
- 1.11.5
- 1.11.4
- 1.11.3
- 1.11.2
- 1.11.1
- 1.11.0
- 1.10.5
- 1.8.9
- 1.8.8
- 1.8.6
- 1.8.5
- 1.8.4
- 1.8.3
- 1.8.1
- 1.8.0
- 1.7.6
- 1.7.5
- 1.7.4
- 1.7.3
- 1.7.0
- dev-dispatcher
- dev-rename/modal-to-record
- dev-configServer
- dev-chunk/index
- dev-sharding/balancer
- dev-sharding/allocate-command
- dev-chunk/clone
- dev-chunk/split
- dev-chunk/move
- dev-repo/import
- dev-sharding/chunk-manager
- dev-config/databases
- dev-sharding/consistent-hasher
- dev-sharding/prune
- dev-migration-refactor
- dev-refactor/database-builder
- dev-collection-loader
- dev-develop
This package is not auto-updated.
Last update: 2024-09-22 21:32:08 UTC
README
4.0.x版本目前正在积极开发中,API不稳定
Maghead是一个开源的对象关系映射(ORM),为PHP7设计。
Maghead使用静态代码生成器生成映射到数据库记录和方法的静态类,从而降低运行时成本,因此它非常轻量级且速度极快。
通过简单的模式设计,您可以轻松地定义模型模式,甚至可以在模式类中嵌入闭包。
它有多快?目前它是用纯PHP编写的最快的ORM。 查看基准测试以获取更多详情。
自动迁移演示
特性
- 快速且简单
- 基于YAML格式的配置,并编译成PHP
- 支持PDO、MySQL、Pgsql、SQLite。
- 多个数据源。
- 混合模型。
- 强大的迁移生成器
- 升级和降级当然。
- 自动迁移:根据模式差异自动生成迁移SQL。
- 模式/数据库差异
设计概念
-
PHP中的函数调用非常慢,因此模型模式数据将静态构建,Maghead将所有定义(默认值、验证器、过滤器、有效值构建器)转换为类和静态PHP数组,这保持了这些模型类非常轻量级且速度快。
-
在运行时,所有相同的模型对象使用相同的模式对象,我们可以重用静态模式类中预先构建的数据。
-
我们保留基础模型类构造函数为空,因此当您从数据库查询数据时,这些模型对象可以轻松创建。
入门
请参阅Wiki中的详细说明。
模式
定义模式类
简单地从Maghead\Schema\DeclareSchema
扩展类,并在schema
方法中定义您的模型列,例如:
<?php namespace TestApp; use Maghead\Schema\DeclareSchema; class BookSchema extends DeclareSchema { public function schema() { $this->column('title') ->unique() ->varchar(128); $this->column('subtitle') ->varchar(256); $this->column('isbn') ->varchar(128) ->immutable(); $this->column('description') ->text(); $this->column('view') ->default(0) ->integer(); $this->column('publisher_id') ->isa('int') ->integer(); $this->column('published_at') ->isa('DateTime') ->timestamp(); $this->column('created_by') ->integer() ->refer('TestApp\UserSchema'); // Defining trait for model class $this->addModelTrait('Uploader'); $this->addModelTrait('Downloader') ->useInsteadOf('Downloader::a', 'Uploader'); $this->belongsTo('created_by', 'TestApp\UserSchema','id', 'created_by'); /** * column: author => Author class * * $book->publisher->name; * **/ $this->belongsTo('publisher','\TestApp\PublisherSchema', 'id', 'publisher_id'); /** * accessor , mapping self.id => BookAuthors.book_id * * link book => author_books */ $this->many('book_authors', '\TestApp\AuthorBookSchema', 'book_id', 'id'); /** * get BookAuthor.author */ $this->manyToMany( 'authors', 'book_authors', 'author' ) ->filter(function($collection) { return $collection; }); } }
定义列类型
$this->column('foo')->integer(); $this->column('foo')->float(); $this->column('foo')->varchar(24); $this->column('foo')->text(); $this->column('foo')->binary();
文本
$this->column('name')->text();
布尔值
$this->column('name') ->boolean();
整数
$this->column('name')->integer();
时间戳
$this->column('name')->timestamp();
日期时间
$this->column('name')->datetime();
定义混合方法
namespace Maghead\Schema\Mixin; use Maghead\Schema\MixinDeclareSchema; class MetadataMixinSchema extends MixinDeclareSchema { public function schema() { // ... define your schema here } public function fooMethod($record, $arg1, $arg2, $arg3, $arg4) { // ... return ...; } }
然后您可以在模型对象上使用fooMethod
$result = $record->fooMethod(1,2,3,4);
使用多个数据源
您可以在模型模式中为不同的模型定义特定的数据源
use Maghead\Schema\DeclareSchema; class UserSchema extends DeclareSchema { public function schema() { $this->writeTo('master'); $this->readFrom('slave'); } }
或者您可以指定为两者(读取和写入)
use Maghead\Schema\DeclareSchema; class UserSchema extends DeclareSchema { public function schema() { $this->using('master'); } }
迁移
如果您需要修改模式代码,例如向表中添加新列,您可以使用出色的迁移功能轻松地将数据库迁移到最新更改。
一旦修改了模式代码,您就可以执行lazy diff
命令来比较当前存在的数据库表
$ maghead diff
+ table 'authors' tests/tests/Author.php
+ table 'addresses' tests/tests/Address.php
+ table 'author_books' tests/tests/AuthorBook.php
+ table 'books' tests/tests/Book.php
+ table 'users' tests/tests/User.php
+ table 'publishers' tests/tests/Publisher.php
+ table 'names' tests/tests/Name.php
+ table 'wines' tests/tests/Wine.php
如您所见,我们添加了大量的新表(模式),并且Maghead解析数据库表以向您显示差异,让您了解当前状态。
目前Maghead支持SQLite、PostgreSQL、MySQL表解析。
现在您可以直接生成迁移脚本或升级数据库模式。
要直接升级数据库模式,您可以简单运行
$ maghead migrate auto
要通过可定制的迁移脚本升级数据库模式,您可以生成新的迁移脚本,例如
$ maghead migrate diff AddUserRoleColumn
Loading schema objects...
Creating migration script from diff
Found 10 schemas to compare.
Found schema 'TestApp\AuthorSchema' to be imported to 'authors'
Found schema 'TestApp\AddressSchema' to be imported to 'addresses'
Found schema 'TestApp\AuthorBookSchema' to be imported to 'author_books'
Found schema 'TestApp\BookSchema' to be imported to 'books'
Found schema 'TestApp\UserSchema' to be imported to 'users'
Found schema 'TestApp\PublisherSchema' to be imported to 'publishers'
Found schema 'TestApp\NameSchema' to be imported to 'names'
Found schema 'TestApp\Wine' to be imported to 'wines'
Migration script is generated: db/migrations/20120912_AddUserRoleColumn.php
现在您可以编辑自动生成的迁移脚本
vim db/migrations/20120912_AddUserRoleColumn.php
迁移脚本看起来像这样
class AddUserColumn_1347451491 extends \Maghead\Migration\Migration { public function upgrade() { $this->importSchema(new TestApp\AuthorSchema); $this->importSchema(new TestApp\AddressSchema); // To upgrade with new schema: $this->importSchema(new TestApp\AuthorBookSchema); // To create index: $this->createIndex($table,$indexName,$columnNames); // To drop index: $this->dropIndex($table,$indexName); // To add a foreign key: $this->addForeignKey($table,$columnName,$referenceTable,$referenceColumn = null) // To drop table: $this->dropTable('authors'); } public function downgrade() { $this->dropTable('authors'); $this->dropTable('addresses'); } }
内置的迁移生成器不仅生成升级脚本,还生成降级脚本,您可以随意修改。
生成迁移脚本后,您可以检查当前数据库的状态和等待迁移的脚本。
$ maghead migrate status
Found 1 migration script to be executed.
- AddUserColumn_1347451491
现在您可以通过迁移脚本来运行升级命令升级数据库模式。
$ maghead migrate up
如果您后悔,可以通过命令运行降级迁移。
$ maghead migrate down
但请注意,SQLite 不支持列重命名和列删除。
要了解迁移脚本能做什么,请查看 Magsql 包的文档。
更高级的模式模型
use Maghead\Schema\DeclareSchema; class AuthorSchema extends DeclareSchema { function schema() { $this->column('id') ->integer() ->primary() ->autoIncrement(); $this->column('name') ->varchar(128) ->validator(function($val) { .... }) ->filter( function($val) { return preg_replace('#word#','zz',$val); }) ->inflator(function($val) { return unserialize($val); }) ->deflator(function($val) { return serialize($val); }) ->validValues( 1,2,3,4,5 ) ->default(function() { return date('c'); }) ; $this->column('email') ->required() ->varchar(128); $this->column('confirmed') ->default(false) ->boolean(); $this->seeds('User\\Seed') } }
许可证
MIT 许可证