lox/pheasant
最新稳定版本:v1.3.2
Composer 安装命令:
composer require lox/pheasant
包简介
A lightweight data mapper for MySQL and PHP 7.2+
README 文档
README
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.
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
lox/pheasant 适用场景与选型建议
lox/pheasant 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 18.48k 次下载、GitHub Stars 达 101, 最近一次更新时间为 2012 年 05 月 31 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「orm」 「mysql」 「activerecord」 「datamapper」 「99designs」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 lox/pheasant 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 lox/pheasant 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 lox/pheasant 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Dibi is Database Abstraction Library for PHP
ActiveRecord for API
CalendarView widget for Yii 2 Framework.
Kinikit - PHP Application development framework MVC component
PHP Database ORM for Symfony1. Do NOT use for new projects: please move to a newest Symfony release and Doctrine2
Relational attributes support for YII2 ActiveRecord
统计信息
- 总下载量: 18.48k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 107
- 点击次数: 26
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2012-05-31