承接 joedevsharp/entitylite 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

joedevsharp/entitylite

最新稳定版本:0.2.2

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

  1. Installation
  2. Database Connection
  3. DbContext
  4. Entity and DbSet
  5. BaseRepository
  6. Example Usage

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.

统计信息

  • 总下载量: 15
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: Unknown
  • 更新时间: 2024-09-14