PHPackages                             maghead/maghead - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. maghead/maghead

ActiveLibrary[Database &amp; ORM](/categories/database)

maghead/maghead
===============

The Fast PHP ORM

4.0.1(3y ago)47984326[99 issues](https://github.com/maghead/maghead/issues)4MITPHPPHP &gt;=5.6CI failing

Since Aug 29Pushed 3mo ago22 watchersCompare

[ Source](https://github.com/maghead/maghead)[ Packagist](https://packagist.org/packages/maghead/maghead)[ Docs](https://github.com/maghead/maghead)[ RSS](/packages/maghead-maghead/feed)WikiDiscussions master Synced 2mo ago

READMEChangelogDependencies (19)Versions (113)Used By (4)

Maghead
=======

[](#maghead)

[![Build Status](https://camo.githubusercontent.com/5a073d9798b0780180c3d8c647a84bfa9d4442bad29b5c43ff22c0a2aea3aa34/68747470733a2f2f7472617669732d63692e6f72672f6d6167686561642f6d6167686561642e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/maghead/maghead)[![Coverage Status](https://camo.githubusercontent.com/a8eb5227f98c6d94904d6a88ea4820fc9ade58f494b78bb9d903f027766f1e17/68747470733a2f2f696d672e736869656c64732e696f2f636f766572616c6c732f6d6167686561642f6d6167686561642e737667)](https://coveralls.io/r/maghead/maghead)[![Latest Stable Version](https://camo.githubusercontent.com/610a48b994c5823c432c8abaf4c71ae2387b1bad1de8bcf50e7e18f9ce72765d/68747470733a2f2f706f7365722e707567782e6f72672f6d6167686561642f6d6167686561642f762f737461626c652e737667)](https://packagist.org/packages/maghead/maghead)[![Total Downloads](https://camo.githubusercontent.com/5e71495ad32d7e668a90e576f7b81db97d661e02ad60747f063e2b2f486a41e9/68747470733a2f2f706f7365722e707567782e6f72672f6d6167686561642f6d6167686561642f646f776e6c6f6164732e737667)](https://packagist.org/packages/maghead/maghead)[![Monthly Downloads](https://camo.githubusercontent.com/11d89e3a796121ae181e2c6d2e2c69e994fc93634d309026245e00bb6626fb5b/68747470733a2f2f706f7365722e707567782e6f72672f6d6167686561642f6d6167686561642f642f6d6f6e74686c79)](https://packagist.org/packages/maghead/maghead)[![Daily Downloads](https://camo.githubusercontent.com/8a9c9a040fc7b132cfd50f4312f116f4cbc94d8d39d499b25696cce1f92e368c/68747470733a2f2f706f7365722e707567782e6f72672f6d6167686561642f6d6167686561642f642f6461696c79)](https://packagist.org/packages/maghead/maghead)[![Latest Unstable Version](https://camo.githubusercontent.com/0709e183ed94654b64d951012ba2d3cbe451f1b194963a51b3929acbf105509e/68747470733a2f2f706f7365722e707567782e6f72672f6d6167686561642f6d6167686561642f762f756e737461626c652e737667)](https://packagist.org/packages/maghead/maghead)[![License](https://camo.githubusercontent.com/f378a3a994207e0fd7bb41e8ed05e195e5437be76f3a43d21cc36a75a6b9245f/68747470733a2f2f706f7365722e707567782e6f72672f6d6167686561642f6d6167686561642f6c6963656e73652e737667)](https://packagist.org/packages/maghead/maghead)[![Join the chat at https://gitter.im/maghead/maghead](https://camo.githubusercontent.com/a4e422870c642bd65cf6929079242d21a3669704a0788acafeb0ff2d8c4da80a/68747470733a2f2f6261646765732e6769747465722e696d2f6d6167686561642f6d6167686561642e737667)](https://gitter.im/maghead/maghead?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)[![Works On My Machine](https://camo.githubusercontent.com/71c24a0850c0404418232a97a1a7e217d4d6377b7de352c76edb8e88458b7878/68747470733a2f2f63646e2e7261776769742e636f6d2f6e696b6b752f776f726b732d6f6e2d6d792d6d616368696e652f76302e322e302f62616467652e737667)](https://github.com/nikku/works-on-my-machine)[![Made in Taiwan](https://camo.githubusercontent.com/9ff8461912d454a52a3c2a22a80da9a976140bdb7125bb360d029ebeb755985f/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6d616465253230696e2d74616977616e2d677265656e2e737667)](README.md)

Maghead is an open-source Object-Relational Mapping (ORM) designed for PHP7.

Maghead uses static code generator to generate static classes that maps to the database records and methods, which reduces runtime costs, therefore it's pretty lightweight and extremely fast.

With the simple schema design, you can define your model schema very easily and you can even embed closure in your schema classes.

How 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).

Automatic Migration Demonstration
---------------------------------

[](#automatic-migration-demonstration)

[![](https://camo.githubusercontent.com/3ea139133d3dcc774b1c53175259c66a05208f344321cd0980af40a252836deb/68747470733a2f2f7261772e6769746875622e636f6d2f6d6167686561642f6d6167686561642f6d61737465722f6173736574732f696d616765732f6d6967726174696f6e2e676966)](https://camo.githubusercontent.com/3ea139133d3dcc774b1c53175259c66a05208f344321cd0980af40a252836deb/68747470733a2f2f7261772e6769746875622e636f6d2f6d6167686561642f6d6167686561642f6d61737465722f6173736574732f696d616765732f6d6967726174696f6e2e676966)

Feature
-------

[](#feature)

- Fast &amp; Simple
- Configuration based on YAML format and compiled into PHP
- PDO, MySQL, Pgsql, SQLite support.
- Multiple data sources.
- Mix-in model.
- Powerful Migration Generator
    - Upgrade &amp; Downgrade of course.
    - Automatic Migration: generate migration SQL automatically based on the schema diff.
- Schema/Database diff

Design Concept
--------------

[](#design-concept)

- Function calls in PHP are very slow, so the model schema data will be built statically, Maghead converts all definitions (default value, validator, filter, valid value builder) into classes and static PHP array, this keeps these model classes very lightweight and fast.
- In the runtime, all the same model objects use the same schema object, and we can reuse the prebuild data from the static schema class.
- We keep base model class constructor empty, so when you are querying data from database, these model objects can be created with zero effort.

Getting Started
---------------

[](#getting-started)

Please see the details on [Wiki](https://github.com/maghead/maghead/wiki)

Schema
------

[](#schema)

### Defining Schema Class

[](#defining-schema-class)

Simply extend class from `Maghead\Schema\DeclareSchema`, and define your model columns in the `schema` method, e.g.,

```
