migliori/power-lite-pdo
Composer 安装命令:
composer require migliori/power-lite-pdo
包简介
PowerLitePdo is a PDO wrapper that provides a simple and powerful way to interact with a database.
README 文档
README
PowerLite PDO is a lightweight, powerful PHP library that provides a simple and efficient way to interact with databases using PHP Data Objects (PDO). It supports multiple database drivers and includes features like easy connection management, query execution, result handling and pagination.
Table of Contents
- Features
- Requirements
- Documentation
- Installation
- Configuration
- Usage/Examples
- Running Tests
- Contributing
- License
Features
- Containerized Connections: The containers are used to connect your database and handle the configuration and dependencies seamlessly.
- Db & QueryBuilder Methods: Execute SQL queries and retrieve results using Db methods or the fluent QueryBuilder.
- Efficient Result Handling: Fetch single row, all rows, or specific column's value with dedicated methods.
- Comprehensive Database Operations: Provides a wide range of methods for diverse database interactions.
- Pagination Support: Handle paginated results effortlessly with the Pagination class.
- Error Management: User-friendly handling of database errors and exceptions.
- Debug Mode: Provides detailed information about the requests for debugging purposes.
- Prepared Statements: Support for prepared statements to prevent SQL injection attacks.
- Transaction Control: Manage database transactions with methods to start, commit, and rollback.
- High Code Quality Standards: The code follows best practices and coding standards.
Requirements
PHP ^7.4, PHP 8.x
Documentation
The documentation for PowerLite PDO is available on the PowerLite PDO website.
In addition to the documentation, a PHPDoc is also available here for more detailed information about the classes, methods, and their parameters.
Installation
Clone / download or install with Composer
composer require migliori/power-lite-pdo
Configuration
Open src/connection.php in your code editor and replace the constant's values with your database connection settings (DB_HOST, DB_NAME, DB_USER, DB_PASS, DB_PORT, DB_CHARSET).
Security concerns
For enhanced safety, store the file outside of your web server's document root (the directory that is served to the internet) and change the path accordingly in the configuration file (src/config.php). This prevents the file from being directly accessible via a URL.
Usage/Examples
Select records using the main Db class
-
Include the bootstrap file and get the Db instance from the container:
use Migliori\PowerLitePdo\Db; // Build the container and connect to the database $container = require_once __DIR__ . '/vendor/migliori/power-lite-pdo/src/bootstrap.php'; $db = $container->get(Db::class);
-
Use the select method from the Db class to select some records:
$from = 'users'; // The table name $fields = ['id', 'username', 'email']; // The columns you want to select $where = ['status' => 'active']; // The conditions for the WHERE clause $db->select($from, $fields, $where);
-
Fetch the selected records one by one:
while ($record = $db->fetch()) { echo $record->id . ', ' . $record->username . ', ' . $record->email . "\n"; }
Select records using the fluent QueryBuilder
-
Include the bootstrap file and get the QueryBuilder instance from the container:
use Migliori\PowerLitePdo\Query\QueryBuilder; // Build the container and connect to the database $container = require_once __DIR__ . '/vendor/migliori/power-lite-pdo/src/bootstrap.php'; $queryBuilder = $container->get(QueryBuilder::class);
-
Use the QueryBuilder to select some records:
$queryBuilder->select(['id', 'username', 'email'])->from('users')->where(['status' => 'active'])->execute();
-
Fetch the selected records one by one:
while ($record = $queryBuilder->fetch()) { echo $record->id . ', ' . $record->username . ', ' . $record->email . "\n"; }
Select records with Pagination
-
Include the bootstrap file and get the Pagination instance from the container:
use Migliori\PowerLitePdo\Pagination; // Build the container and connect to the database $container = require_once __DIR__ . '/vendor/migliori/power-lite-pdo/src/bootstrap.php'; $pagination = $container->get(Pagination::class);
-
Select some records:
$from = 'users'; // The table name $fields = ['id', 'username', 'email']; // The columns you want to select $where = ['status' => 'active']; // The conditions for the WHERE clause $pagination->select($from, $fields, $where);
-
Fetch the selected records one by one:
while ($record = $pagination->fetch()) { echo $record->id . ', ' . $record->username . ', ' . $record->email . "\n"; }
-
Display the pagination:
$url = '/users'; // The URL for the pagination links echo $pagination->pagine($url);
Running Tests
To run tests, run the following command
php ./vendor/bin/phpunit test
Dependencies
- Composer: A dependency management tool for PHP.
- PHP-DI: A dependency injection container for PHP.
- PDO: The PHP Data Objects extension for accessing databases.
- Database Drivers: The specific drivers for the databases you want to connect to (e.g., MySQL, PostgreSQL, Oracle, Firebird, ...).
Dev Dependencies
- PHPUnit: A testing framework for unit testing PHP code.
- PHPStan: A static analysis tool that helps find bugs in PHP code.
- PHP CodeSniffer: A set of rules to ensure that PHP code follows coding standards.
Contributing
Contributions are always welcome!
Please contact us for any improvement suggestions or send your pull requests
License
migliori/power-lite-pdo 适用场景与选型建议
migliori/power-lite-pdo 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 83 次下载、GitHub Stars 达 15, 最近一次更新时间为 2024 年 04 月 12 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「database」 「pdo」 「wrapper」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 migliori/power-lite-pdo 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 migliori/power-lite-pdo 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 migliori/power-lite-pdo 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Dibi is Database Abstraction Library for PHP
Store your language lines in the database, yaml or other sources
LinkedIn API PHP SDK with OAuth 2.0 & CSRF support. Can be used for social sign in or sharing on LinkedIn. Examples. Documentation.
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
LinkedIn API PHP SDK with OAuth 2.0 & CSRF support. Can be used for social sign in or sharing on LinkedIn. Examples. Documentation.
A PSR-7 compatible library for making CRUD API endpoints
统计信息
- 总下载量: 83
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 15
- 点击次数: 16
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: GPL-3.0-or-later
- 更新时间: 2024-04-12