martynbiz/database
Composer 安装命令:
composer require martynbiz/database
包简介
Collection of database classes to allow database operations and unit testing
README 文档
README
#Database
Light weight ORM classes. It's designed to be well suited for unit testing where the database adapter can be mocked easily. Also provides just enough to do common tasks such as find/select/create/update/delete, and associations between tables (e.g. hasMany/ belongsTo). Can be run stand along or within a framework.
##Installation
Install with composer:
"martynbiz/database": "dev-master"
##ORM
Models/User.php
class User extends MartynBiz\Database\Table { protected $table = 'users'; }
Script
$adapter = new MartynBiz\Database\Adapter(array( 'dsn' => '...', 'user' => '...', 'password' => '...', )); $usersTable = Users::getInstance($adapter); // return Row object $user = $usersTable->find($id); // select many rows by query (with options) $where = 'age > 34'; $options = array( 'limitStart' => 10, 'limitMax' => 25, ); $users = $usersTable->select($where, $options); // insert new row $values = array( 'name' => 'Hugo', 'age' => 54, ); $users->create($values); // update rows $usersTable->update($values, $where, $options); // delete rows $usersTable->delete($where, $options);
###Associations
class User extends Table { protected $tableName = 'users'; protected $hasMany = array( 'transactions' => array( 'table' => 'App\Model\Transaction', // class name for the hasMany rows 'foreign_key' => 'user_id', ) ); } class Transaction extends Table { protected $tableName = 'transactions'; protected $belongsTo = array( 'user' => array( 'table' => 'App\Model\User', // class name for the belongsTo row 'foreign_key' => 'user_id', ) ); }
##Database adapter
...
##Unit testing
###Table
$adapterMock = $this->getMockBuilder('MartynBiz\Database\Adapter') ->disableOriginalConstructor() ->getMock(); $usersTable = new Users($adapterMock);
###Adapter
If it was neccessary to extend the adapter class, the PDO object which is usually generated internally can be injected instead (upon which the database credentials will be ingored). This allows the extended class to be unit testing with a mock PDO object.
class MyCustomAdapter extands Adapter { } $PDOMock->expects( $this->once() ) ->method('prepare') ->with($sql); $PDOStatementMock->expects( $this->once() ) ->method('execute') ->with($whereValues); $PDOStatementMock->expects( $this->once() ) ->method('fetchAll') ->will( $this->returnValue($mockExecuteResult) ); // create instance with mock $adapter = new MyCustomAdapter(null, $PDOMock);
TODO
- cascade
martynbiz/database 适用场景与选型建议
martynbiz/database 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 14 次下载、GitHub Stars 达 0, 最近一次更新时间为 2014 年 11 月 09 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「database」 「database adapter」 「table gateway」 「row gateway」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 martynbiz/database 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 martynbiz/database 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 martynbiz/database 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Dibi is Database Abstraction Library for PHP
Adapter designed to add Framework Agnosticism for Caching within PHP Packages.
flysystem cache Adapter
Plug-ins for DataTables
Virtual Filesystem Storage Adapter for Laravel
A component that allows creating responsive HTML tables or lists from data object
统计信息
- 总下载量: 14
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 13
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2014-11-09