kora/database
Composer 安装命令:
composer require kora/database
包简介
Database for the Kora Framework.
README 文档
README
A simple and efficient way to interact with MySQL/MariaDB databases.
- MySQLDatabase: A secure PDO-based database wrapper for MySQL/MariaDB.
- Repository: A convenient CRUD and basic query-building layer on top of MySQLDatabase.
Installation
Install via Composer:
composer require kora/database
Quickstart
1. Autoload
After installing, ensure you include Composer’s autoloader:
require_once __DIR__ . '/vendor/autoload.php';
2. Create a MySQLDatabase instance
use Kora\Database\MySQL\MySQLDatabase; $db = new MySQLDatabase( host: '127.0.0.1', dbName: 'your_database', user: 'your_user', password: 'your_password', port: 3306 // default is 3306, can omit if desired );
- SSL Support: If your environment requires a secure connection, pass
useSSL: trueand provide SSL options. - Logging: Optionally pass a PSR-3 compatible logger for debug/error logging.
3. Perform Basic Queries
// Example: Insert a row $rows = $db->execute( "INSERT INTO users (username, email) VALUES (:username, :email)", ['username' => 'alice', 'email' => 'alice@example.com'] ); echo "Rows inserted: {$rows}\n"; // Fetch a single row $user = $db->fetchOne( "SELECT * FROM users WHERE username = :username", ['username' => 'alice'] ); var_dump($user); // Fetch multiple rows $users = $db->fetchAll("SELECT * FROM users"); foreach ($users as $user) { echo $user['username'], PHP_EOL; }
4. Using the Repository Class
The Repository offers a high-level CRUD interface plus a mini query builder.
use Kora\Database\MySQL\Repository; // Create a repository for the "users" table, primary key is "id" by default $userRepo = new Repository($db, 'users', 'id'); // Create a new user $newUserId = $userRepo->create([ 'username' => 'bob', 'email' => 'bob@example.com', 'status' => 'active', ]); // Read a user $bob = $userRepo->find($newUserId); echo "Found user: " . $bob['username'] . "\n"; // Update the user $userRepo->update($newUserId, ['status' => 'inactive']); // Delete the user $userRepo->delete($newUserId);
Query Builder Examples
// Get all active users, ordered by creation time $activeUsers = $userRepo ->where('status', '=', 'active') ->orderBy('created_at', 'DESC') ->get(); // Count how many users have a certain status $inactiveCount = $userRepo ->where('status', '=', 'inactive') ->count(); // Check if any records exist with username "alice" $aliceExists = $userRepo ->where('username', '=', 'alice') ->exists();
5. Transactions
Use the underlying MySQLDatabase transactions for multi-step operations:
$db->transaction(function (MySQLDatabase $db) { // Perform multiple queries that succeed or fail together $db->execute("UPDATE accounts SET balance = balance - 100 WHERE user_id = 1"); $db->execute("UPDATE accounts SET balance = balance + 100 WHERE user_id = 2"); });
Next Steps
- Explore the rich methods in both
MySQLDatabaseandRepository(likefetchColumn,tableExists, etc.).
That’s it! You now have a simple convenient way to manage MySQL/MariaDB data.
kora/database 适用场景与选型建议
kora/database 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2 次下载、GitHub Stars 达 1, 最近一次更新时间为 2025 年 01 月 20 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 kora/database 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 kora/database 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 2
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 19
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-01-20