phore/orm
Composer 安装命令:
composer require phore/orm
包简介
Object Relational Mapper
README 文档
README
Overview
Phore MiniSql is a lightweight ORM library for PHP, providing an easy-to-use interface for database operations such as create, read, update, delete (CRUD), and schema management. This README provides detailed usage instructions, including entity definitions, handling operations, indexes, foreign keys, and table/connection maintenance.
Installation
To install Phore MiniSql, use Composer:
composer require phore/minisql
Defining Entities
Entities are defined as PHP classes with public properties representing the columns of the corresponding database table. Each entity class must implement a static __schema method that returns an OrmClassSchema object.
namespace App\Entity; use Phore\MiniSql\Schema\OrmClassSchema; class User { public int $id; public string $name; public string $email; public static function __schema(): OrmClassSchema { return new OrmClassSchema( tableName: 'users', primaryKey: 'id', autoincrement: true, columns: [ 'id' => 'int', 'name' => 'varchar(255)', 'email' => 'varchar(255)' ] ); } }
Usage
Connecting to the Database
To connect to the database, create an instance of the Orm class and provide the DSN and entity classes.
use Phore\MiniSql\Orm; use App\Entity\User; $orm = new Orm([User::class], 'mysql:host=localhost;dbname=testdb;user=root;password=root'); $orm->connect(); $orm->updateSchema();
Creating Records
To create a new record, instantiate the entity class, set its properties, and call the create method.
$user = new User(); $user->name = 'John Doe'; $user->email = 'john.doe@example.com'; $orm->create($user);
Reading Records
To read a record by its primary key, use the read method.
$user = $orm->withClass(User::class)->read(1);
Updating Records
To update a record, modify its properties and call the update method.
$user->name = 'Jane Doe'; $orm->update($user);
Deleting Records
To delete a record, call the delete method.
$orm->delete($user);
Listing All Records
To list all records of an entity, use the listAll method.
$users = $orm->withClass(User::class)->listAll();
Selecting Records with Conditions
To select records with specific conditions, use the select method.
$users = $orm->withClass(User::class)->select(['name' => 'Jane Doe']);
Indexes
Indexes can be defined in the OrmClassSchema using the indexes property.
public static function __schema(): OrmClassSchema { return new OrmClassSchema( tableName: 'users', primaryKey: 'id', autoincrement: true, columns: [ 'id' => 'int', 'name' => 'varchar(255)', 'email' => 'varchar(255)' ], indexes: [ new OrmIndex( columns: ['email'], type: 'UNIQUE' ), new OrmIndex( columns: ['hit_id', "tag"], indexName: 'idx_hit_id_tag', type: 'INDEX' ) ], foreignKeys: [ new OrmForeignKey("hit_id", Hit::class, "id", onDelete: "cascade") ] ); }
Foreign Keys
Foreign keys can be defined in the OrmClassSchema using the foreignKeys property.
use Phore\MiniSql\Schema\OrmForeignKey; public static function __schema(): OrmClassSchema { return new OrmClassSchema( tableName: 'orders', primaryKey: 'id', autoincrement: true, columns: [ 'id' => 'int', 'user_id' => 'int', 'product_id' => 'int' ], foreignKeys: [ new OrmForeignKey('user_id', 'users', 'id'), new OrmForeignKey('product_id', 'products', 'id') ] ); }
Maintaining Tables and Connections
Updating Schema
To update the database schema based on the defined entities, use the updateSchema method.
$orm->updateSchema();
Dropping All Tables
To drop all tables in the database, use the dropAllTables method.
$orm->getDriver()->getSchemaUpdater()->dropAllTables();
Conclusion
Phore MiniSql provides a simple and efficient way to manage database operations in PHP. By defining entities and using the provided methods, you can easily perform CRUD operations, manage indexes and foreign keys, and maintain your database schema.
phore/orm 适用场景与选型建议
phore/orm 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 104 次下载、GitHub Stars 达 1, 最近一次更新时间为 2024 年 07 月 10 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 phore/orm 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 phore/orm 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 104
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 19
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-07-10