piko/db-record
最新稳定版本:v2.2.2
Composer 安装命令:
composer require piko/db-record
包简介
A lightweight Active Record helper built on top of PDO.
README 文档
README
Piko Db Record is a lightweight Active Record implementation built on top of PDO.
It has been tested and works with the following databases:
- SQLite
- MySQL
- PostgreSQL
- MSSQL
Installation
It's recommended that you use Composer to install Piko Db Record.
composer require piko/db-record
Documentation
https://piko-framework.github.io/docs/db-record.html
Usage
First, ensure you have the necessary autoloading in place:
require 'vendor/autoload.php';
Define Your Model
Use the Piko\DbRecord, Piko\DbRecord\Attribute\Table, and Piko\DbRecord\Attribute\Column
classes to define your model. For example:
use Piko\DbRecord; use Piko\DbRecord\Attribute\Table; use Piko\DbRecord\Attribute\Column; #[Table(name:'contact')] class Contact extends DbRecord { #[Column(primaryKey: true)] public ?int $id = null; #[Column] public $name = null; #[Column] public ?int $order = null; }
Setup Database Connection
Create a new PDO instance and set up your database schema:
// See https://www.php.net/manual/en/class.pdo.php $db = new PDO('sqlite::memory:'); $query = <<<EOL CREATE TABLE contact ( id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, `order` INTEGER ) EOL; $db->exec($query);
Perform CRUD Operations
Create
Create a new record and save it to the database:
$contact = new Contact($db); $contact->name = 'John'; $contact->order = 1; $contact->save(); echo "Contact id : {$contact->id}"; // Contact id : 1
Read
Retrieve records from the database:
$st = $db->prepare('SELECT * FROM contact'); $st->execute(); $rows = $st->fetchAll(PDO::FETCH_CLASS, Contact::class, [$db]); print_r($rows); // Array ([0] => Contact Object(...)) // Load a single record by primary key: $contact = (new Contact($db))->load(1); var_dump($contact->name); // John
Delete
Delete a record from the database:
$contact->delete(); print_r($st->fetchAll()); // Array()
Support
If you encounter any issues or have questions, feel free to open an issue on GitHub.
统计信息
- 总下载量: 76
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: LGPL-3.0-or-later
- 更新时间: 2022-11-04