joedevsharp/entitylite

There is no license information available for the latest version (0.2.2) of this package.

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

0.2.2 2024-10-01 04:14 UTC

This package is auto-updated.

Last update: 2025-07-29 02:18:35 UTC


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.