{"id":14973957,"url":"https://github.com/maghead/maghead","last_synced_at":"2025-04-12T20:45:49.256Z","repository":{"id":2944557,"uuid":"3957736","full_name":"maghead/maghead","owner":"maghead","description":"The fastest pure PHP database framework with a powerful static code generator, supports horizontal scale up, designed for PHP7","archived":false,"fork":false,"pushed_at":"2022-08-15T09:15:11.000Z","size":14787,"stargazers_count":479,"open_issues_count":99,"forks_count":27,"subscribers_count":23,"default_branch":"master","last_synced_at":"2025-04-04T00:09:41.606Z","etag":null,"topics":["database","model-schema","mysql","orm","performance","pgsql","php","sqlite"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/maghead.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2012-04-07T12:55:08.000Z","updated_at":"2025-03-15T20:58:21.000Z","dependencies_parsed_at":"2022-07-29T22:49:28.218Z","dependency_job_id":null,"html_url":"https://github.com/maghead/maghead","commit_stats":null,"previous_names":[],"tags_count":93,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maghead%2Fmaghead","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maghead%2Fmaghead/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maghead%2Fmaghead/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maghead%2Fmaghead/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/maghead","download_url":"https://codeload.github.com/maghead/maghead/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248631692,"owners_count":21136559,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["database","model-schema","mysql","orm","performance","pgsql","php","sqlite"],"created_at":"2024-09-24T13:49:44.449Z","updated_at":"2025-04-12T20:45:49.229Z","avatar_url":"https://github.com/maghead.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"Maghead\n==========\n\n[![Build Status](https://travis-ci.org/maghead/maghead.svg?branch=master)](https://travis-ci.org/maghead/maghead)\n[![Coverage Status](https://img.shields.io/coveralls/maghead/maghead.svg)](https://coveralls.io/r/maghead/maghead)\n[![Latest Stable Version](https://poser.pugx.org/maghead/maghead/v/stable.svg)](https://packagist.org/packages/maghead/maghead) \n[![Total Downloads](https://poser.pugx.org/maghead/maghead/downloads.svg)](https://packagist.org/packages/maghead/maghead) \n[![Monthly Downloads](https://poser.pugx.org/maghead/maghead/d/monthly)](https://packagist.org/packages/maghead/maghead)\n[![Daily Downloads](https://poser.pugx.org/maghead/maghead/d/daily)](https://packagist.org/packages/maghead/maghead)\n[![Latest Unstable Version](https://poser.pugx.org/maghead/maghead/v/unstable.svg)](https://packagist.org/packages/maghead/maghead) \n[![License](https://poser.pugx.org/maghead/maghead/license.svg)](https://packagist.org/packages/maghead/maghead)\n[![Join the chat at https://gitter.im/maghead/maghead](https://badges.gitter.im/maghead/maghead.svg)](https://gitter.im/maghead/maghead?utm_source=badge\u0026utm_medium=badge\u0026utm_campaign=pr-badge\u0026utm_content=badge)\n[![Works On My Machine](https://cdn.rawgit.com/nikku/works-on-my-machine/v0.2.0/badge.svg)](https://github.com/nikku/works-on-my-machine)\n[![Made in Taiwan](https://img.shields.io/badge/made%20in-taiwan-green.svg)](README.md)\n\n**4.0.x IS CURRENTLY UNDER HEAVY DEVELOPMENT, API IS NOT STABLE**\n\nMaghead is an open-source Object-Relational Mapping (ORM) designed for PHP7.\n\nMaghead uses static code generator to generate static classes that maps to the database records and methods, which reduces runtime\ncosts, therefore it's pretty lightweight and extremely fast.\n\nWith the simple schema design, you can define your model schema very easily and\nyou can even embed closure in your schema classes.\n\nHow fast is it? Currently it's the fastest ORM written in pure PHP. [See the benchmark for more details](https://github.com/c9s/forked-php-orm-benchmark).\n\nAutomatic Migration Demonstration\n--------------------------------\n\u003cimg src=\"https://raw.github.com/maghead/maghead/master/assets/images/migration.gif\" width=\"600\"/\u003e\n\nFeature\n-------\n\n* Fast \u0026 Simple\n* Configuration based on YAML format and compiled into PHP\n* PDO, MySQL, Pgsql, SQLite support.\n* Multiple data sources.\n* Mix-in model.\n* Powerful Migration Generator\n  * Upgrade \u0026 Downgrade of course.\n  * Automatic Migration: generate migration SQL automatically based on the schema diff.\n* Schema/Database diff\n\nDesign Concept\n--------------\n\n- Function calls in PHP are very slow, so the model schema data\n  will be built statically, Maghead converts all definitions (default value, validator, filter, valid\n  value builder) into classes and static PHP array, this keeps these model\n  classes very lightweight and fast.\n  \n- In the runtime, all the same model objects use the same \n  schema object, and we can reuse the prebuild data from the static schema class.\n\n- We keep base model class constructor empty, so when you are querying data from\n  database, these model objects can be created with zero effort.\n\nGetting Started\n---------------\n\nPlease see the details on [Wiki](https://github.com/maghead/maghead/wiki)\n\n\nSchema\n------------\n\n### Defining Schema Class\n\nSimply extend class from `Maghead\\Schema\\DeclareSchema`, and define your model columns \nin the `schema` method, e.g.,\n\n```php\n\u003c?php\nnamespace TestApp;\nuse Maghead\\Schema\\DeclareSchema;\n\nclass BookSchema extends DeclareSchema\n{\n\n    public function schema()\n    {\n        $this-\u003ecolumn('title')\n            -\u003eunique()\n            -\u003evarchar(128);\n\n        $this-\u003ecolumn('subtitle')\n            -\u003evarchar(256);\n\n        $this-\u003ecolumn('isbn')\n            -\u003evarchar(128)\n            -\u003eimmutable();\n\n        $this-\u003ecolumn('description')\n            -\u003etext();\n\n        $this-\u003ecolumn('view')\n            -\u003edefault(0)\n            -\u003einteger();\n\n        $this-\u003ecolumn('publisher_id')\n            -\u003eisa('int')\n            -\u003einteger();\n\n        $this-\u003ecolumn('published_at')\n            -\u003eisa('DateTime')\n            -\u003etimestamp();\n\n        $this-\u003ecolumn('created_by')\n            -\u003einteger()\n            -\u003erefer('TestApp\\UserSchema');\n\n\n        // Defining trait for model class\n        $this-\u003eaddModelTrait('Uploader');\n        $this-\u003eaddModelTrait('Downloader')\n            -\u003euseInsteadOf('Downloader::a', 'Uploader');\n\n        $this-\u003ebelongsTo('created_by', 'TestApp\\UserSchema','id', 'created_by');\n\n        /** \n         * column: author =\u003e Author class \n         *\n         * $book-\u003epublisher-\u003ename;\n         *\n         **/\n        $this-\u003ebelongsTo('publisher','\\TestApp\\PublisherSchema', 'id', 'publisher_id');\n\n        /**\n         * accessor , mapping self.id =\u003e BookAuthors.book_id\n         *\n         * link book =\u003e author_books\n         */\n        $this-\u003emany('book_authors', '\\TestApp\\AuthorBookSchema', 'book_id', 'id');\n\n\n        /**\n         * get BookAuthor.author \n         */\n        $this-\u003emanyToMany( 'authors', 'book_authors', 'author' )\n            -\u003efilter(function($collection) { return $collection; });\n    }\n}\n```\n\n### Defining Column Types\n\n```php\n$this-\u003ecolumn('foo')-\u003einteger();\n$this-\u003ecolumn('foo')-\u003efloat();\n$this-\u003ecolumn('foo')-\u003evarchar(24);\n$this-\u003ecolumn('foo')-\u003etext();\n$this-\u003ecolumn('foo')-\u003ebinary();\n```\n\nText:\n\n```php\n$this-\u003ecolumn('name')-\u003etext();\n```\n\nBoolean:\n\n```php\n$this-\u003ecolumn('name') -\u003eboolean();\n```\n\nInteger:\n\n```php\n$this-\u003ecolumn('name')-\u003einteger();\n```\n\nTimestamp:\n\n```php\n$this-\u003ecolumn('name')-\u003etimestamp();\n```\n\nDatetime:\n\n```php\n$this-\u003ecolumn('name')-\u003edatetime();\n```\n\n\n\n\n#### Defining Mixin Method\n\n```php\nnamespace Maghead\\Schema\\Mixin;\nuse Maghead\\Schema\\MixinDeclareSchema;\n\nclass MetadataMixinSchema extends MixinDeclareSchema\n{\n    public function schema()\n    {\n        // ... define your schema here\n    }\n\n    public function fooMethod($record, $arg1, $arg2, $arg3, $arg4)\n    {\n        // ...\n        return ...;\n    }\n}\n```\n\nThen you can use the `fooMethod` on your model object:\n\n```php\n$result = $record-\u003efooMethod(1,2,3,4);\n```\n\n### Using Multiple Data Source\n\nYou can define specific data source for different model in the model schema:\n\n```php\nuse Maghead\\Schema\\DeclareSchema;\n\nclass UserSchema extends DeclareSchema {\n\n    public function schema() {\n        $this-\u003ewriteTo('master');\n        $this-\u003ereadFrom('slave');\n    }\n\n}\n```\n\nOr you can specify for both (read and write):\n\n```php\nuse Maghead\\Schema\\DeclareSchema;\n\nclass UserSchema extends DeclareSchema {\n\n    public function schema() {\n        $this-\u003eusing('master');\n    }\n\n}\n```\n\n\nMigration\n---------\n\nIf you need to modify schema code, like adding new columns to a table, you \ncan use the amazing migration feature to migrate your database to the latest\nchange without pain.\n\nOnce you modified the schema code, you can execute `lazy diff` command to compare\ncurrent exisiting database table:\n\n    $ maghead diff\n    + table 'authors'            tests/tests/Author.php\n    + table 'addresses'          tests/tests/Address.php\n    + table 'author_books'       tests/tests/AuthorBook.php\n    + table 'books'              tests/tests/Book.php\n    + table 'users'              tests/tests/User.php\n    + table 'publishers'         tests/tests/Publisher.php\n    + table 'names'              tests/tests/Name.php\n    + table 'wines'              tests/tests/Wine.php\n\nAs you can see, we added a lot of new tables (schemas), and Maghead parses\nthe database tables to show you the difference to let you know current\nstatus.\n\n\u003e Currently Maghead supports SQLite, PostgreSQL, MySQL table parsing.\n\nnow you can generate the migration script or upgrade database schema directly.\n\nto upgrade database schema directly, you can simply run:\n\n    $ maghead migrate auto\n\nto upgrade database schema through a customizable migration script, you can \ngenerate a new migration script like:\n\n    $ maghead migrate diff AddUserRoleColumn\n    Loading schema objects...\n    Creating migration script from diff\n    Found 10 schemas to compare.\n        Found schema 'TestApp\\AuthorSchema' to be imported to 'authors'\n        Found schema 'TestApp\\AddressSchema' to be imported to 'addresses'\n        Found schema 'TestApp\\AuthorBookSchema' to be imported to 'author_books'\n        Found schema 'TestApp\\BookSchema' to be imported to 'books'\n        Found schema 'TestApp\\UserSchema' to be imported to 'users'\n        Found schema 'TestApp\\PublisherSchema' to be imported to 'publishers'\n        Found schema 'TestApp\\NameSchema' to be imported to 'names'\n        Found schema 'TestApp\\Wine' to be imported to 'wines'\n    Migration script is generated: db/migrations/20120912_AddUserRoleColumn.php\n\nnow you can edit your migration script, which is auto-generated:\n\n    vim db/migrations/20120912_AddUserRoleColumn.php\n\nthe migration script looks like:\n\n```php\nclass AddUserColumn_1347451491  extends \\Maghead\\Migration\\Migration {\n\n    public function upgrade() { \n        $this-\u003eimportSchema(new TestApp\\AuthorSchema);\n        $this-\u003eimportSchema(new TestApp\\AddressSchema);\n\n        // To upgrade with new schema:\n        $this-\u003eimportSchema(new TestApp\\AuthorBookSchema);\n        \n        // To create index:\n        $this-\u003ecreateIndex($table,$indexName,$columnNames);\n        \n        // To drop index:\n        $this-\u003edropIndex($table,$indexName);\n        \n        // To add a foreign key:\n        $this-\u003eaddForeignKey($table,$columnName,$referenceTable,$referenceColumn = null) \n        \n        // To drop table:\n        $this-\u003edropTable('authors');\n    }\n\n    public function downgrade() { \n\n        $this-\u003edropTable('authors');\n        $this-\u003edropTable('addresses');\n        \n    }\n}\n```\n\nThe built-in migration generator not only generates the upgrade script,\nbut also generates the downgrade script, you can modify it to anything as you\nwant.\n\nAfter the migration script is generated, you can check the status of \ncurrent database and waiting migration scripts:\n\n    $ maghead migrate status\n    Found 1 migration script to be executed.\n    - AddUserColumn_1347451491\n\nnow you can run upgrade command to \nupgrade database schema through the migration script:\n\n    $ maghead migrate up\n\nIf you regret, you can run downgrade migrations through the command:\n\n    $ maghead migrate down\n\nBut please note that SQLite doesn't support column renaming and column\ndropping.\n\nTo see what migration script could do, please check the documentation of\nMagsql package.\n\n### A More Advanced Model Schema\n\n```php\nuse Maghead\\Schema\\DeclareSchema;\n\nclass AuthorSchema extends DeclareSchema\n{\n    function schema()\n    {\n        $this-\u003ecolumn('id')\n            -\u003einteger()\n            -\u003eprimary()\n            -\u003eautoIncrement();\n\n        $this-\u003ecolumn('name')\n            -\u003evarchar(128)\n            -\u003evalidator(function($val) { .... })\n            -\u003efilter( function($val) {  \n                        return preg_replace('#word#','zz',$val);  \n            })\n            -\u003einflator(function($val) {\n                return unserialize($val);\n            })\n            -\u003edeflator(function($val) {\n                return serialize($val);\n            })\n            -\u003evalidValues( 1,2,3,4,5 )\n            -\u003edefault(function() { \n                return date('c');\n            })\n            ;\n\n        $this-\u003ecolumn('email')\n            -\u003erequired()\n            -\u003evarchar(128);\n\n        $this-\u003ecolumn('confirmed')\n            -\u003edefault(false)\n            -\u003eboolean();\n\n        $this-\u003eseeds('User\\\\Seed')\n    }\n}\n```\n\n\nLICENSE\n===============\n\nMIT License\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmaghead%2Fmaghead","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmaghead%2Fmaghead","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmaghead%2Fmaghead/lists"}