定制 bentools/simple-dbal 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

bentools/simple-dbal

Composer 安装命令:

composer require bentools/simple-dbal

包简介

README 文档

README

Latest Stable Version License Quality Score Total Downloads

SimpleDBAL

A modern wrapper on top of PDO and Mysqli, written in PHP7.1. It aims at exposing the same API regardless of the one your project uses.

Introduction

PHP offers 2 different APIs to connect to a SQL database: PDO and mysqli (we won't talk about mysql_* functions since they're deprecated). Both do more or less the same thing, but:

  • Their API are completely different (different method names and signatures)
  • They both have their own pros and cons.
  • Some features of one are missing in the other.

This library exposes an API that can be used by any of them, transparently, in a more modern approach (OOP, iterators, return types, etc).

This also means you can switch from PDO to mysqli and vice-versa without having to rewrite your whole code.

From my personnal experience, I was used to PDO and was forced to deal with mysqli in another project and it was really messy.

Overview

use BenTools\SimpleDBAL\Model\Credentials;
use BenTools\SimpleDBAL\Model\SimpleDBAL;

$credentials = new Credentials('localhost', 'user', 'password', 'database');
$cnx         = SimpleDBAL::factory($credentials, SimpleDBAL::PDO);
$query       = "SELECT `id`, `name` FROM guys WHERE created_at > ?";
foreach ($cnx->execute($query, [new DateTime('-1 month')]) as $item) {
    var_dump($item['name']);
}

Additionnal features

  • On-the-fly parameter binding for prepared statements (simply pass an array of arguments after the query)
  • DateTimeInterface objects automatic binding (formats to YYYY-MM-DD HH:ii:ss)
  • Asynchronous queries (Promises)
  • Parallel queries
  • Support for named parameters in prepared statements for mysqli (polyfill with regexps - experimental feature)
  • Can silently reconnect after a lost connection

The Result object

Every query sent to the adapter will return a BenTools\SimpleDBAL\Contract\ResultInterface object.

For SELECT queries, use the following methods:

  • $result->asArray() to fetch an array containing the whole resultset
  • $result->asRow() to fetch the 1st row of the resultset, as an associative array
  • $result->asList() to fetch the 1st column of the resultset, as a sequential array
  • $result->asValue() to fetch a single value (i.e the 1st column of the 1st row)
  • foreach ($result as $row) to iterate over the resultset (uses lazy-loading)
  • count($result) to return the number of rows of the resultset.

For INSERT / UPDATE / DELETE queries, use the following methods:

  • count($result) to return the number of affected rows
  • $result->getLastInsertId() to get the id of the last inserted row or sequence value.

Installation

composer require bentools/simple-dbal

Tests

./vendor/bin/phpunit

Documentation

Getting started

Configuring auto-reconnect

Asynchronous and parallel queries

Connection pools

Known Issues

统计信息

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

GitHub 信息

  • Stars: 3
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

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

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固