flightphp/active-record
Composer 安装命令:
composer require flightphp/active-record
包简介
Micro Active Record library in PHP, support chain calls, events, and relations.
README 文档
README
An active record is mapping a database entity to a PHP object. Spoken plainly, if you have a users table in your database, you can "translate" a row in that table to a User class and a $user object in your codebase. See basic example.
Basic Example
Let's assume you have the following table:
CREATE TABLE users ( id INTEGER PRIMARY KEY, name TEXT, password TEXT );
Now you can setup a new class to represent this table:
/** * An ActiveRecord class is usually singular * * It's highly recommended to add the properties of the table as comments here * * @property int $id * @property string $name * @property string $password */ class User extends flight\ActiveRecord { public function __construct($databaseConnection) { parent::__construct($databaseConnection, 'users', [/* custom values */]); } }
Now watch the magic happen!
// for sqlite $database_connection = new PDO('sqlite:test.db'); // this is just for example, you'd probably use a real database connection // for mysql $database_connection = new PDO('mysql:host=localhost;dbname=test_db&charset=utf8bm4', 'username', 'password'); // or mysqli $database_connection = new mysqli('localhost', 'username', 'password', 'test_db'); // or mysqli with non-object based creation $database_connection = mysqli_connect('localhost', 'username', 'password', 'test_db'); $user = new User($database_connection); $user->name = 'Bobby Tables'; $user->password = password_hash('some cool password'); $user->insert(); // or $user->save(); echo $user->id; // 1 $user->name = 'Joseph Mamma'; $user->password = password_hash('some cool password again!!!'); $user->insert(); echo $user->id; // 2
And it was just that easy to add a new user! Now that there is a user row in the database, how do you pull it out?
$user->find(1); // find id = 1 in the database and return it. echo $user->name; // 'Bobby Tables'
And what if you want to find all the users?
$users = $user->findAll();
What about with a certain condition?
$users = $user->like('name', '%mamma%')->findAll();
See how much fun this is? Let's install it and get started!
Installation
Simply install with Composer
composer require flightphp/active-record
Documentation
Head over to the documentation page to learn more about usage and how cool this thing is! :)
License
MIT
flightphp/active-record 适用场景与选型建议
flightphp/active-record 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4.06k 次下载、GitHub Stars 达 16, 最近一次更新时间为 2024 年 01 月 17 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「database」 「orm」 「Active Record」 「micro」 「pdo」 「Simple」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 flightphp/active-record 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 flightphp/active-record 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 flightphp/active-record 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Dibi is Database Abstraction Library for PHP
Propel2 is an open-source Object-Relational Mapping (ORM) for PHP 5.5 and up.
Kinikit - PHP Application development framework MVC component
Store your language lines in the database, yaml or other sources
A Symfony extension to get active class base on current bundle/controller/action
PHP Database ORM for Symfony1. Do NOT use for new projects: please move to a newest Symfony release and Doctrine2
统计信息
- 总下载量: 4.06k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 16
- 点击次数: 4
- 依赖项目数: 11
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-01-17