2017 © Pedro Peláez
 

library pheasant

A lightweight data mapper for MySQL and PHP 5.3+

image

lox/pheasant

A lightweight data mapper for MySQL and PHP 5.3+

  • Tuesday, January 30, 2018
  • by lox
  • Repository
  • 12 Watchers
  • 104 Stars
  • 17,573 Installations
  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 19 Forks
  • 16 Open issues
  • 19 Versions
  • 0 % Grown

The README.md

Pheasant

Pheasant is an object relational mapper written to take advantage of PHP 7. Simple relationships are supported, with the emphasis being on scalability and performance over complexity., (*1)

Pheasant doesn't attempt to abstract the database and makes heavy use of MySQL/Innodb features., (*2)

Status of Development

Running in production on 99designs.com. See ROADMAP for more details on future plans., (*3)

Build Status, (*4)

Installing

Easiest way is to install via composer http://packagist.org/packages/lox/pheasant., (*5)

composer require lox/pheasant

Persisting Objects

Each domain object has a set of properties and relationships that are defined in the configure method. Each domain object delegates to a mapper object for the actual saving and loading of objects., (*6)

<?php

use \Pheasant;
use \Pheasant\Types;

class Post extends DomainObject
{
  public function properties()
  {
    return array(
      'postid'   => new Types\SequenceType(),
      'title'    => new Types\StringType(255, 'required'),
      'subtitle' => new Types\StringType(255),
      'status'   => new Types\EnumType(array('closed','open')),
      'authorid' => new Types\IntegerType(11),
      );
  }

  public function relationships()
  {
    return array(
      'Author' => Author::hasOne('authorid')
    );
  }
}

class Author extends DomainObject
{
  public function properties()
  {
    return array(
      'authorid' => new Types\SequenceType(),
      'fullname' => new Types\StringType(255, 'required')
    );
  }

  public function relationships()
  {
    return array(
      'Posts' => Post::hasOne('authorid')
    );
  }
}

// configure database connection
Pheasant::setup('mysql://localhost:/mydatabase');

// you can add extra options:
// Pheasant::setup('mysql://root@localhost:/mydatabase?charset=utf8'); (utf8 is assumed by default)
// Pheasant::setup('mysql://root:password@localhost:/mydatabase?ssl_ca=foobar.pem');

// create some objects
$author = new Author(array('fullname'=>'Lachlan'));
$post = new Post(array('title'=>'My Post', 'author'=>$author));

// save objects
$author->save();
$post->save();

echo $post->title; // returns 'My Post'
echo $post->Author->fullname; // returns 'Lachlan'

Magical Finders

Many variations of finders are available for locating objects:, (*7)

<?php

// all users
$users = User::all();

// all users named frank
$users = User::find('firstname = ?', 'frank');

// any fields can be used in finders, this translates to above
$users = User::findByFirstName('frank');

// a single user named frank
$users = User::one('firstname = ?', 'frank');

// a user by primary key
$user = User::byId(1);

// all comments by a user (if user hasmany comment)
$comments = User::byId(1)->Comment;

// to prevent the n+1 query issue, eager load the relation:
$users = User::all()->includes(['Comment']); // $users[0]->Comment will not hit db

// eager loading also has support for eager loading sub-relations
$users = User::all()->includes(['Comment' => [ 'Like', ]]);

Collection Scoping

Scoping allows you to specify commonly-used queries which can be referenced as method calls on Collection objects. All scope methods will return a Pheasant::Collection object which will allow for further methods (such as other scopes) to be called on it., (*8)

To define a simple scope, we first define a scopes method in our DomainObject that returns an associative array in "methodName" => $closure form., (*9)

use \Pheasant;
Class User extends DomainObject
{
  public function scopes()
  {
    return array(
      'active' => function($collection){
        $collection->filter('last_login_date >= ?', strtotime('30 days ago'));
      },
    );
  }
}

// Scopes may be used by invoking them like methods
User::all()->active()
//=> Returns all active users

Events

Code can be triggered before and after create, update and delete operations., (*10)

<?php

use \Pheasant;
use \Pheasant\Events;
use \Pheasant\Types;

class Post extends DomainObject
{
  public function properties()
  {
    return array(
      'postid'      => new Types\SequenceType(),
      'title'       => new Types\StringType(255),
      'timecreated' => new Types\IntegerType(11),
      ));
  }

  public function beforeCreate($post)
  {
    $d->timecreated = time();
  }
}

Optionally, domain objects provide the following implicit hooks which can be overriden:, (*11)

  • afterCreate
  • beforeUpdate, afterUpdate

Transactions

Transactions can be created globally:, (*12)

<?php


\Pheasant::transaction(function() {
  $post = new Post(array('title'=>'First Post!'));
  $post->save();
});

Or transactions can be invoked on an instance:, (*13)

<?php

$post = new Post(array('title'=>'First Post!'));

$post->transaction(function($obj) {
  $obj->save();
});

Contributors

Many thanks to @dhotson, @michaeldewildt, @rbone, @harto, @jorisleker, @tombb, @Jud, @bjornpost, @creativej, (*14)

The Versions

30/01 2018

dev-master

9999999-dev

A lightweight data mapper for MySQL and PHP 5.3+

  Sources   Download

MIT

The Requires

  • php >=5.6.0

 

The Development Requires

by Richard Bone

orm mysql 99designs activerecord datamapper

05/05 2015

v1.3.2

1.3.2.0

A lightweight data mapper for MySQL and PHP 5.3+

  Sources   Download

MIT

The Requires

  • php >=5.3.2

 

The Development Requires

by Richard Bone

orm mysql 99designs activerecord datamapper

10/09 2014

dev-hhvm-in-travis

dev-hhvm-in-travis

A lightweight data mapper for MySQL and PHP 5.3+

  Sources   Download

MIT

The Requires

  • php >=5.3.2

 

The Development Requires

by Richard Bone

orm mysql 99designs activerecord datamapper

08/09 2014

v1.3.1

1.3.1.0

A lightweight data mapper for MySQL and PHP 5.3+

  Sources   Download

MIT

The Requires

  • php >=5.3.2

 

The Development Requires

by Richard Bone

orm mysql 99designs activerecord datamapper

28/05 2014

v1.3.0

1.3.0.0

A lightweight data mapper for MySQL and PHP 5.3+

  Sources   Download

MIT

The Requires

  • php >=5.3.2

 

The Development Requires

by Richard Bone

orm mysql 99designs activerecord datamapper

08/05 2014

dev-bugs/109

dev-bugs/109

A lightweight data mapper for MySQL and PHP 5.3+

  Sources   Download

MIT

The Requires

  • php >=5.3.2

 

The Development Requires

by Richard Bone

orm mysql 99designs activerecord datamapper

21/01 2014

v1.2.1

1.2.1.0

A lightweight data mapper for MySQL and PHP 5.3+

  Sources   Download

MIT

The Requires

  • php >=5.3.2

 

The Development Requires

by Richard Bone

orm mysql 99designs activerecord datamapper

20/01 2014

v1.2.0

1.2.0.0

A lightweight data mapper for MySQL and PHP 5.3+

  Sources   Download

MIT

The Requires

  • php >=5.3.2

 

The Development Requires

by Richard Bone

orm mysql 99designs activerecord datamapper

07/10 2013

v1.1.2

1.1.2.0

A lightweight data mapper for MySQL and PHP 5.3+

  Sources   Download

MIT

The Requires

  • php >=5.3.2

 

The Development Requires

by Richard Bone

orm mysql 99designs activerecord datamapper

08/05 2013

v1.1.1

1.1.1.0

A lightweight data mapper for MySQL and PHP 5.3+

  Sources   Download

MIT

The Requires

  • php >=5.3.2

 

The Development Requires

by Richard Bone

orm mysql 99designs activerecord datamapper

19/04 2013

v1.1.0

1.1.0.0

A lightweight data mapper for MySQL and PHP 5.3+

  Sources   Download

MIT

The Requires

  • php >=5.3.2

 

The Development Requires

by Richard Bone

orm mysql 99designs activerecord datamapper

03/01 2013

v1.0.0

1.0.0.0

A lightweight data mapper for MySQL and PHP 5.3+

  Sources   Download

MIT

The Requires

  • php >=5.3.2

 

The Development Requires

by Richard Bone

orm mysql 99designs activerecord datamapper

21/11 2012

v1.0.0-beta5

1.0.0.0-beta5

A lightweight data mapper for MySQL and PHP 5.3+

  Sources   Download

MIT

The Requires

  • php >=5.3.2

 

The Development Requires

by Richard Bone

orm mysql 99designs activerecord datamapper

15/11 2012

v1.0.0-beta4

1.0.0.0-beta4

A lightweight data mapper for MySQL and PHP 5.3+

  Sources   Download

MIT

The Requires

  • php >=5.3.2

 

The Development Requires

by Richard Bone

orm mysql 99designs activerecord datamapper

14/11 2012

dev-phpunit

dev-phpunit

A lightweight data mapper for MySQL and PHP 5.3+

  Sources   Download

MIT

The Requires

  • php >=5.3.2

 

The Development Requires

by Richard Bone

orm mysql 99designs activerecord datamapper

15/08 2012

v1.0.0-beta3

1.0.0.0-beta3

A lightweight data mapper for MySQL and PHP 5.3+

  Sources   Download

MIT

The Requires

  • php >=5.3.2

 

The Development Requires

by Richard Bone

orm mysql 99designs activerecord datamapper

09/06 2012

dev-mocks

dev-mocks

A lightweight data mapper for MySQL and PHP 5.3+

  Sources   Download

MIT

The Requires

  • php >=5.3.2

 

by Richard Bone

orm mysql 99designs activerecord datamapper

06/06 2012

v1.0.0-beta2

1.0.0.0-beta2

A lightweight data mapper for MySQL and PHP 5.3+

  Sources   Download

MIT

The Requires

  • php >=5.3.2

 

by Richard Bone

orm mysql 99designs activerecord datamapper

31/05 2012

v1.0.0-beta1

1.0.0.0-beta1

A lightweight data mapper for MySQL and PHP 5.3+

  Sources   Download

MIT

The Requires

  • php >=5.3.2

 

by Richard Bone

orm mysql 99designs activerecord datamapper