romagny13/micro-db 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

romagny13/micro-db

Composer 安装命令:

composer require romagny13/micro-db

包简介

MicroPHP DB library

README 文档

README

Installation

composer require romagny13/micro-db

Usage

Configure the database connection.

Example:

$settings = [
    'dsn' =>"mysql:host=localhost;dbname=blog",
    'username'=>'root',
    'password' =>''
];
Db::setConnectionStringSettings($settings['dsn'],$settings['username'],$settings['password']);

Sql tables/columns Strategy

By default the columns are wrapped with back ticks

Example:

select `posts`.`id`,`title`,`content`,`users`.`id`,`users`.`username` from `posts`,`users` order by `title`

Change the strategy. Example:

Db::setTableAndColumnStrategy('[',']');
select [posts].[id],[title],[content],[users].[id],[users].[username] from [posts],[users] order by [title]

With the Query Builder:

  • select
  • insert_into
  • update
  • delete

Examples:

Select

$posts = Db::getInstance()
    ->select('id','title','content','user_id')
    ->from('posts')
    ->where(Condition::op('user_id',1))
    ->orderBy('title')
    ->limit(10)
    ->fetchAll();

Other example, fetch a class

class Post { }

$posts = Db::getInstance()
    ->select('posts.id','title','content','user_id','users.id','users.username')
    ->from('posts','users')
    ->where('user_id=1')
    ->orderBy(Sort::desc('title'),'content desc')
    ->limit(2,10)
    ->fetchAll(Post::class);

Get the querystring :

$queryString = Db::getInstance()->select('posts.id','title','content','user_id','users.id','users.username')
    ->from('posts','users')
    ->where('user_id=1')
    ->orderBy(Sort::desc('title'),'content desc')
    ->limit(2,10)
    ->build();

var_dump($queryString);
select `posts`.`id`,`title`,`content`,`user_id`,`users`.`id`,`users`.`username` from `posts`,`users` where user_id=1 order by `title` desc,`content` desc limit 2,10

Insert

$success = Db::getInstance()
    ->insert_into('posts')
    ->columns('title','content','user_id')
    ->execute(['my title','my content',1]);

Or

$success = Db::getInstance()
    ->insert_into('posts')
    ->columns('title','content','user_id')
    ->values('my title','my content',1)
    ->execute();

and get the last inserted id

$id = Db::getInstance()->lastInsertId();

Update

$success = Db::getInstance()
    ->update('posts')
    ->set([
        'title'=>'new title',
        'content' => 'new content'
    ])
    ->where(Condition::op('id',1))
    ->execute();

Delete

$success = Db::getInstance()
    ->delete_from('posts')
    ->where(Condition::op('id',1))
    ->execute();

Condition helper

  • op
  • in
  • between
  • like

plus chaining conditions with:

  • and
  • or

PDO

Simple query

$posts = Db::getInstance()
    ->query('select * from posts')
    ->fetchAllWithClass(Post::class);

Query with params

$posts = Db::getInstance()
    ->prepare('select * from posts where id=:id')
    ->setParam(':id',1)
    ->fetchObject(Post::class);

Other example

$success = Db::getInstance()
    ->prepare('insert into posts (title,content,user_id) values (?,?,?)')
    ->execute(['My title', 'My content',2]);

$id = Db::getInstance()->lastInsertId();

Or with named params

$success = Db::getInstance()
    ->prepare('insert into posts (title,content,user_id) values (:title,:content,:user_id)')
    ->setParam(':title','My title')
    ->setParam(':content','My content')
    ->setParam(':user_id',2)
    ->execute();

$id = Db::getInstance()->lastInsertId();

Model

Create a model and define the db table. By default all columns will be filled ['*'].

use \MicroPHP\Db\Model;

class PostModel extends Model
{
    public function __construct()
    {
        $this->table = 'posts';
    }
}

Define the columns to fill

use \MicroPHP\Db\Model;

class PostModel extends Model
{
    public function __construct()
    {
        $this->table = 'posts';
        $this->columns = ['id','title','content','user_id'];
    }
}

All

get all the records of the table

$posts = PostModel::all();

With limit

Example: only 10 posts (maximum) will be returned

$posts = PostModel::all(10);

With offset + limit

Example: only 10 posts (maximum) will be returned after 2 posts

$posts = PostModel::all(2,10);

Where

Allow to select the records to return (array)

$posts = PostModel::where(Condition::op('user_id',1)->_or_(Condition::op('user_id',2)));

or with a string

$posts = PostModel::where('user_id=1 or user_id=2');

With offset + limit

$posts = PostModel::where('user_id=1 or user_id=2',2,10);

Find

Returns only one item.

$post = PostModel::find(Condition::op('id',1));

Create

Example:

$success = PostModel::create([
    'title' => 'My title',
    'content' => 'My content',
    'user_id' => 1
]);

Update

Example:

$success = PostModel::update([
    'title' => 'My new title',
    'content' => 'My new content'
],Condition::op('id',1));

Delete

Example:

$success = PostModel::delete(Condition::op('id',1));

Query and prepare

Are shortcuts to Db functions.

Relations

Add relations (0-1) or (1-1) to other models

class UserModel extends Model
{
    public function __construct()
    {
        $this->table = 'users';
    }
}

class CategoryModel extends Model
{
    public function __construct()
    {
        $this->table = 'categories';
    }
}

class PostModel extends Model
{
    public function __construct()
    {
        $this->table = 'posts';
        $this->columns = ['title','content'];

        // relations
        $this->addRelation('users',['user_id' => 'id'],UserModel::class, 'user');
        $this->addRelation('categories',['category_id' => 'id'],CategoryModel::class, 'category');
    }
}

addRelation parameters:

  • foreign key table name
  • foreign key => primary key pairs
  • model to fill
  • property name to add

User and category properties will be added to the post model.

Example:

$post = PostModel::find(Condition::op('id',1));

romagny13/micro-db 适用场景与选型建议

romagny13/micro-db 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 22 次下载、GitHub Stars 达 0, 最近一次更新时间为 2017 年 05 月 05 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「db」 「micro」 「microphp」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 romagny13/micro-db 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 romagny13/micro-db 我们能提供哪些服务?
定制开发 / 二次开发

基于 romagny13/micro-db 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-05-05