lox/pheasant

此包已废弃,不再维护。未建议替代包。

MySQL和PHP 7.2+的轻量级数据映射器

v1.3.2 2015-05-05 09:28 UTC

README

Pheasant是一个利用PHP 7编写的对象关系映射器。支持简单的关联,强调可扩展性和性能,而非复杂性。

Pheasant不试图抽象数据库,并大量使用了MySQL/InnoDB的特性。

开发状态

在99designs.com上运行生产环境。有关未来计划的更多详细信息,请参阅ROADMAP

Build Status

安装

最简单的方法是通过composer安装https://packagist.org.cn/packages/lox/pheasant

composer require lox/pheasant

对象持久化

每个领域对象都有一组属性和关系,这些属性和关系在配置方法中定义。每个领域对象委托给映射器对象以进行实际的对象保存和加载。

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

神奇查找器

许多finders的变体可用于定位对象

<?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', ]]);

集合范围

范围允许您指定常用查询,这些查询可以作为集合对象上的方法调用进行引用。所有范围方法都将返回一个Pheasant::Collection对象,这将允许进一步的方法(例如其他范围)被调用。

要定义一个简单的范围,我们首先在我们的DomainObject中定义一个返回关联数组的scopes方法,其形式为"methodName" => $closure

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

事件

可以在创建、更新和删除操作之前和之后触发代码。

<?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();
  }
}

可选地,领域对象提供以下隐式钩子,可以覆盖

  • afterCreate
  • beforeUpdate, afterUpdate

事务

可以全局创建事务

<?php


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

或者可以在实例上调用事务

<?php

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

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

贡献者

非常感谢 @dhotson, @michaeldewildt, @rbone, @harto, @jorisleker, @tombb, @Jud, @bjornpost, @creativej