Software:Maghead (PHP)

From HandWiki
Revision as of 15:17, 9 February 2024 by WikiGary (talk | contribs) (link)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Maghead
Meghead-logo-rev5-512.png
Stable release
4.0.0 / May 17, 2017 (2017-05-17)
Written inPHP
Operating systemCross-platform
TypeObject-relational mapping
LicenseMIT
Websitegithub.com/maghead

"Maghead" is a database framework, written in PHP, which includes libraries for object-relational mapping, database layer abstracting, sharding ... etc.

It provides a bunch of generators that can generate static code from the model schemas. Queries are generated at compile time, therefore the runtime cost is reduced.

The performance almost close to hand written query code with plain PDO statement.

History

The Maghead project was started in August 2012, formerly known as LazyRecord, with the availability of versions PHP version 5. With version 5.3, PHP was finally able to provide a level of support for object-oriented programming that both made projects like Maghead possible and also created a demand for these hitherto-missing components of large-scale, object-oriented architectures.

Feature

  • Support multiple database connections.
  • Support read/write separation.
  • Schema in pure PHP.
  • Support database Instance management.
  • Support application layer sharding: It includes a shard balancer, consistent hashing library, chunk/shard management tools.
  • Maghead generates the repo classes that follows repository pattern
  • Automatic lightweight migration
  • Support upgrade/downgrade migration by manual written migration scripts.

Usage

Entities in Maghead are lightweight PHP Objects that contain persistable properties. A persistable property is an instance variable of the entity that is saved into and retrieved from the database by Maghead's data mapping capabilities - an implementation of the data mapper pattern:

use App\Model\User;

$newUser = User::create([ .... ]);

$newUserInNode2Database = User::repo('node2')->create([ .... ]);

$user = User::masterRepo()->find(12);
echo $user->first_name;
echo $user->getFirstName(); // with inflator (hydrate)

$ret = $user->update([
  'first_name' => 'Alice',
]);
echo "The user with id {$ret->key} has been saved.";

Schema

Maghead provides the declarative schema API to help user define the model schemas, and which was inspired by Jifty DBI

<?php

namespace AuthorBooks\Model;

use Maghead\Schema\DeclareSchema;

class AuthorSchema extends DeclareSchema
{
    public function schema()
    {
        $this->column('name')
            ->varchar(128)
            ->findable()
            ;

        $this->column('email')
            ->required()
            ->findable()
            ->varchar(128);

        $this->column('account_brief')
            ->label('Account Brief')
            ->virtual()
            ->inflator(function ($value, $record) {
                return $record->name . '(' . $record->email . ')';
            });

        $this->column('identity')
            ->unique()
            ->required()
            ->varchar(128)
            ->validator('StringLength', ['min' => 3, 'max' => 64])
            ->findable()
            ;

        $this->column('confirmed')
            ->boolean()
            ->default(false);

        $this->mixin('Maghead\\Schema\\Mixin\\MetadataMixinSchema');

        $this->many('addresses', 'AuthorBooks\Model\AddressSchema', 'author_id', 'id');

        $this->many('unused_addresses', 'AuthorBooks\Model\AddressSchema', 'author_id', 'id')
            ->where()
                ->equal('unused', true);

        $this->many('author_books', 'AuthorBooks\Model\AuthorBookSchema', 'author_id', 'id');

        $this->manyToMany('books', 'author_books', 'book');
    }
}

Performance

Maghead performs "Find by primary key" best in the benchmarks.

The overall performance benchmark is better than Doctrine (PHP) and Propel (PHP).

Community

See also