krubio/perfect-database
Composer 安装命令:
composer require krubio/perfect-database
包简介
SOLID Compliant Database Connection
README 文档
README
Introduction:
These Database Connection Classes are designed to establish a connection with SQLite, MSSQL, Postgres, MongoDB and MySQL databases. These classes provide a common interface for connecting to different databases using PHP Data Objects (PDO) extension This end user documentation and usage instructions will help you to configure and use the SQLiteConnection and MysqlConnection classes in your PHP project.
Installation
You can install the package using Composer. Run the following command in your project directory:
composer require krubio/perfect-database
This will download the package and its dependencies and add the package to your composer.json file.
Usage
Once you have installed the package, you can use the SqliteConnection and MysqlConnection classes by importing them into your code and creating instances of them. Here is an example:
<?php require_once 'vendor/autoload.php'; use PerfectApp\Database\SqliteConnection; use PerfectApp\Database\MysqlConnection; // SQLite configuration $config = [ 'path' => 'path/to/database/file', 'options' => [ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, ], ]; // Create a SQLite connection $sqliteConnection = new SqliteConnection(); $pdo = $sqliteConnection->connect($config); // MySQL configuration $config = [ 'host' => 'localhost', 'port' => 3306, 'dbname' => 'my_database', 'charset' => 'utf8mb4', 'username' => 'my_username', 'password' => 'my_password', 'options' => [ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, ], ]; // Create a MySQL connection $mysqlConnection = new MysqlConnection(); $pdo = $mysqlConnection->connect($config);
Note that the vendor/autoload.php file is required to load the classes from the package using Composer's autoloading feature. If you're not using Composer, you'll need to manually include the files for the classes you want to use.
That's it! You can now use the PDO object returned by the connect method to interact with your database.
Example:
$stmt = $pdo->prepare("SELECT * FROM users WHERE id=:id"); $stmt->bindValue(':id', 1, PDO::PARAM_INT); $stmt->execute(); $user = $stmt->fetch(PDO::FETCH_ASSOC);
Configuration:
To establish a connection with a database using these classes, you need to provide a configuration array with the required parameters. The configuration array must have the following keys:
For SqliteConnection:
- 'path': The path to the SQLite database file.
- 'options': An array of PDO options. (optional)
For MysqlConnection:
- 'host': The MySQL server hostname or IP address.
- 'port': The MySQL server port number.
- 'dbname': The name of the MySQL database.
- 'charset': The character set to use for the connection.
- 'username': The MySQL username.
- 'password': The MySQL password.
- 'options': An array of PDO options. (optional)
Note: The configuration array must contain all the required keys. Otherwise, an InvalidArgumentException will be thrown.
krubio/perfect-database 适用场景与选型建议
krubio/perfect-database 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 811 次下载、GitHub Stars 达 1, 最近一次更新时间为 2023 年 04 月 25 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「mongodb」 「mysql」 「sqlite」 「postgresql」 「mssql」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 krubio/perfect-database 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 krubio/perfect-database 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 krubio/perfect-database 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Dibi is Database Abstraction Library for PHP
Generates Symfony2 documents, forms and CRUD
The tenancy/tenancy database driver for sqlite
Slimmed down concise interfaces and query builder for database queries and transactions which can be layered / decorated.
A package to allow laravel/passport use with mongodb/laravel-mongodb
MongoDB Adapter to be used with Laminas Paginator
统计信息
- 总下载量: 811
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 4
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-04-25