承接 martynbiz/database 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

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

统计信息

  • 总下载量: 14
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 2
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2014-11-09

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固