mavi/dbmanager 问题修复 & 功能扩展

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

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

mavi/dbmanager

最新稳定版本:v1.0.1

Composer 安装命令:

composer require mavi/dbmanager

包简介

Manager for repository tables.

README 文档

README

Manager for repository tables.
Manager is using PDO (PDO is enabled in PHP by default).
Using PHP 8.1.

Init

$dbManager = new DBManager('host', 'db', 'user', 'password');

Selecting data

$result = $dbManager->table('users')
                    ->select('users.id, users.name, users.phone')
              // or ->select(['users.id', 'users.name', 'users.phone'])
                    ->where('users.name LIKE ? AND users.id > ?', 'Ja%', 1)
              // or ->where(['users.name LIKE ?', 'users.id > ?'], 'Ja%', 1)
                    ->orderBy('users.name DESC, users.id ASC')
              // or ->orderBy(['users.name DESC', 'users.id ASC'])
                    ->limit(2, 0)
                    ->rightJoin('orders')
                        ->on('users.id = orders.user_id')
                        ->select('price, date')
                        ->endJoin($dbManager)
                    ->fetchAll();

Joining tables

Tables can be joined composittely:

->innerJoin('orders')
    ->on('user.id = orders.id_uzivatele')
    ->select('orders.products, orders.date')
    ->innerJoin('products')
        ->on('order.product_id = product.id')
        ->select('price, vat')
->endJoin($dbManager)

Join Funcions:

->innerJoin(...)
->leftJoin(...)
->rightJoin(...)
->fullJoin(...)

Fetching data

->fetchSingle(); // Returns single value

$result = $dbManager->table('users')->select('id')->where(...)->fetchSingle(); // Returns 'id'
$result = $dbManager->table('users')->select('id, name, phone')->where(...)->fetchSingle(); // Returns 'id'
$result = $dbManager->table('users')->select('id, name, phone')->where(...)->fetchSingle(1); // Returns 'name'
$result = $dbManager->table('users')->select('id, name, phone')->where(...)->fetchSingle(2); // Returns 'phone'

->fetch(); // Returns single row as array
->fetchAll(); // Returns array of rows with values as associative array
->fetchPairs(); // Returns rows associated with unique key, values in a rows are associative array
    /*
     * fetchPairs example:
     * [
     *   2 => [id => 2, name => ... ]
     *   5 => [id => 5, name => ...]
     * ]
     */

Cashing results

Results can be cached. If the same queries are executed, DBManager will load cached results.

$dbManager->enableCashing($numberOfMaxCachedResults);
  • Default maximum of cached results is 20.
  • If the limit is exausted, first cached result is removed and results shifted.
  • Cashing is effective for tables with many records.

Inserting data

$dbManager->beginTransaction();

try {
   $id = $dbManager->table('users')->insert(['name' => 'Karl', 'email' => 'karl@example.com'])->getId();
   $dbManager->table('orders')->insert(['user_id' => $id, 'price' => 1000, 'currency' => 'CZK', 'date%sql' => 'NOW()']);
   $dbManager->commit();

} catch (\Exception $e) {
   $dbManager->rollback();
}

Updating data

Updates phone for user which his name begins with Kar* and date of birth is 3.2.1991.

$dbManager->table('users')->where('name LIKE ? AND birth_date = ?', 'Kar%', '1991-02-03')->update(['phone' => '+420 555 555 555']);
  • 'Kar%' - string that begins with 'Kar'.
  • '%arl' - string that ends with 'arl'.
  • '%ar%' - string that contains 'ar'.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-07-19