joedevsharp/entitylite
Composer 安装命令:
composer require joedevsharp/entitylite
包简介
A lightweight PHP ORM inspired by Entity Framework, designed to simplify database interactions and provide an intuitive way to manage entities and relationships. This library utilizes a fluent interface and follows the PSR-4 autoloading standard, making it easy to integrate into any PHP project. Wit
README 文档
README
Here is a documentation draft for your EntityLite library in PHP, including code examples to demonstrate how to use its components effectively.
EntityLite Documentation
EntityLite is a lightweight PHP ORM inspired by Entity Framework, designed to simplify database interactions using an object-oriented approach. This documentation provides an overview of the core classes and how to use them.
Table of Contents
Installation
Make sure to include the EntityLite library in your project. If you are using Composer, ensure your composer.json is properly configured.
{
"require": {
"joedevsharp/entitylite": "^1.0"
}
}
composer require joedevsharp/entitylite
Database Connection
To connect to your database, you will use the Database class. It requires the database credentials to establish a connection.
<?php use EntityLite\Database; $database = new Database('localhost', 'username', 'password', 'database_name'); $dbConnection = $database->getConnection();
Constructor Parameters
host: The database host.username: The username for the database.password: The password for the database.dbname: The name of the database.
DbContext
The DbContext class manages database operations and holds references to entity sets.
Adding Entities
You can add entity sets to the DbContext using the addEntity method.
<?php use EntityLite\DbContext; $dbContext = new DbContext($dbConnection); $dbContext->addEntity('users', User::class);
Retrieving Entities
You can access the added entities as properties of the DbContext instance.
$users = $dbContext->users->findAll(); // Retrieves all users
Entity and DbSet
Entity Class
The Entity class serves as a base class for your entities. It includes a method to convert an object’s properties to an array.
<?php namespace EntityLite; abstract class Entity { public function toArray(): array { return get_object_vars($this); } }
DbSet Class
The DbSet class extends the BaseRepository class, allowing for CRUD operations on entities.
<?php use EntityLite\DbSet; class User extends Entity { public $id; public $name; public $email; } // In DbContext $dbContext->addEntity('users', User::class);
BaseRepository
The BaseRepository class provides common methods for database operations, including findAll, findById, insert, update, and delete.
Method Examples
Find All Records
$users = $dbContext->users->findAll();
Find Record by ID
$user = $dbContext->users->findById(1);
Insert Record
$newUser = new User(); $newUser->name = 'John Doe'; $newUser->email = 'john@example.com'; $dbContext->users->insert($newUser);
Update Record
$existingUser = new User(); $existingUser->name = 'Jane Doe'; $existingUser->email = 'jane@example.com'; $dbContext->users->update(1, $existingUser);
Delete Record
$dbContext->users->delete(1);
Example Usage
Here's a complete example of how to use the EntityLite library to manage a User entity.
<?php require 'vendor/autoload.php'; use EntityLite\Database; use EntityLite\DbContext; use EntityLite\User; $database = new Database('localhost', 'username', 'password', 'database_name'); $dbConnection = $database->getConnection(); $dbContext = new DbContext($dbConnection); $dbContext->addEntity('users', User::class); // Insert a new user $newUser = new User(); $newUser->name = 'John Doe'; $newUser->email = 'john@example.com'; $dbContext->users->insert($newUser); // Retrieve all users $users = $dbContext->users->findAll(); foreach ($users as $user) { echo $user->name . " - " . $user->email . "\n"; } // Update a user $existingUser = new User(); $existingUser->name = 'Jane Doe'; $existingUser->email = 'jane@example.com'; $dbContext->users->update(1, $existingUser); // Delete a user $dbContext->users->delete(2);
Conclusion
The EntityLite library provides a simple and effective way to manage database operations in PHP using an object-oriented approach. By following the examples provided in this documentation, you can easily implement your own entities and utilize the available methods for CRUD operations.
For further information and advanced usage, feel free to explore the source code and adapt it to your needs.
joedevsharp/entitylite 适用场景与选型建议
joedevsharp/entitylite 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 15 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 09 月 14 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 joedevsharp/entitylite 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 joedevsharp/entitylite 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 15
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 25
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: Unknown
- 更新时间: 2024-09-14