• This repository has been archived on 26/May/2023
  • Stars
    star
    101
  • Rank 338,166 (Top 7 %)
  • Language
    PHP
  • License
    MIT License
  • Created over 14 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A lightweight data mapper designed to take advantage of PHP 5.3+

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.

Pheasant doesn't attempt to abstract the database and makes heavy use of MySQL/Innodb features.

Status of Development

Running in production on 99designs.com. See ROADMAP for more details on future plans.

Build Status

Installing

Easiest way is to install via composer http://packagist.org/packages/lox/pheasant.

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.

<?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:

<?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.

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

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.

<?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:

  • afterCreate
  • beforeUpdate, afterUpdate

Transactions

Transactions can be created globally:

<?php


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

Or transactions can be invoked on an instance:

<?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

More Repositories

1

httpcache

An RFC7234 compliant golang http.Handler for caching HTTP responses
Go
264
star
2

alfred-github-jump

An alfred plugin to quickly jump to a github repository page
Go
129
star
3

regreph

A tool for detecting performance regressions in PHP codebases
PHP
50
star
4

go-touchid

Go
41
star
5

phpup

Run a minimal apache instance for a given php script or directory
PHP
37
star
6

pass-share

Securely split a master passphrase into shares for recovery post ☠️
HTML
31
star
7

ecsy

A command-line tool for provisioning and managing AWS ECS clusters
Go
25
star
8

octopi

A graph database built on top of mysql using a node adjacency table and joins for traversal.
PHP
19
star
9

bkl

Run Buildkite Pipelines locally
Go
17
star
10

facade

A PHP5 library for accessing files over a variety of protocols via a generic interface
PHP
14
star
11

package-proxy

A reverse proxy for caching package managers
Go
12
star
12

localbtc-php

A PHP5.3 client for localbitcoins.com API
PHP
12
star
13

ec2spot

A tool for analyzing AWS EC2 spot pricing data
Go
11
star
14

alfred-timezone

An alfred workflow for searching timezones
Go
10
star
15

apt-proxy

A caching proxy specifically for apt package caching
Go
9
star
16

inotify-mitm

An LD_PRELOAD hook for relaying network fsevents to Linux
C
8
star
17

parfait

A command-line tool for creating and managing cloudformation stacks.
Go
8
star
18

docker-compose-buildkit

Go
7
star
19

buildkite-test-analytics-go

Golang support for Buildkite Test Analytics
Go
7
star
20

patchwork

Patchwork allows for seeking over io.ReaderAt and io.WriteAt in golang
Go
6
star
21

go-servejs

A golang http.Handler that executes javascript
JavaScript
6
star
22

dotfiles

Lachlan's Dot Files
Shell
5
star
23

babelcoin

A generic api in golang to access and interoperate between various cryptocurrency exchanges
Go
4
star
24

s3-bench

A golang benchmark for PUT and GET for all the s3 regions
Go
3
star
25

govmomi-vmfork

Go
3
star
26

specialk-php

A 5 and 7 card holdem hand evaluator in PHP
PHP
3
star
27

better-gmail-unsubscribe

A chrome extension that adds an unsubscribe link to gmail messages
JavaScript
3
star
28

node-sass-build-alpine-binaries

Shell
2
star
29

alfred-buildkite-jump

An Alfred plugin to quickly jump to a given Buildkite Pipeline
Go
2
star
30

cloudformation-templates

Example CloudFormation templates
Shell
1
star
31

php-orm-benchmark

A benchmark for mysql-based PHP ORM's
PHP
1
star
32

desmoosh

Splits a string without spaces into words
PHP
1
star
33

evaluate

Go
1
star
34

unsplash-go

A client library and CLI for unsplash.com
Go
1
star
35

geoip-docker-image

Geoip Docker Image
Shell
1
star
36

buildkite-accounter

Go
1
star
37

opencoindata

Cryptocoin exchange data for all.
Go
1
star
38

docker-bluejeansrelay

A docker image for Blue Jean's Relay
Shell
1
star
39

chdocker

Easily download and switch to different versions of docker, docker-compose and docker-machine
Shell
1
star
40

bash-shellwords

Tokenize a string into words, respecting quotes
Shell
1
star
41

macstadium-guides

1
star
42

kitewrk

Benchmark lots of parallel Buildkite jobs
Go
1
star
43

windows-2016-vmware-packer

PowerShell
1
star
44

sysbox-packer

A packer template for building a sysbox base image
Shell
1
star