PHP code example of eril / db-model-entity

1. Go to this page and download the library: Download eril/db-model-entity library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

eril / db-model-entity example snippets


use Eril\DBME\Database\DB;

// Option A: Direct Connection
DB::connect('mysql:host=127.0.0.1;dbname=app_db;charset=utf8mb4', 'root', 'secret');

// Option B: Lazy-Loading Connection Resolver (e.g., sharing a framework instance)
DB::registerConnection(function() {
    return Container::get('pdo');
});

// Option C: Lazy-Loading Connection Resolver (e.g., Own Database Class)
DB::registerConnection([Database::class, "getConnection"]);


use Eril\DBME\ModelEntity;

// Create a new record (Returns an Entity object)
$user = ModelEntity::table('users')->create([
    'name' => 'John Doe',
    'email' => '[email protected]',
    'status' => 'pending'
]);

// Update multiple target records based on conditions
ModelEntity::table('users')
    ->where('status', '=', 'pending')
    ->update(['status' => 'inactive']);

// Delete target records safely
ModelEntity::table('users')->delete(15); // Delete by ID


use Eril\DBME\ModelEntity;

// Fetch a collection using fluent criteria
$users = ModelEntity::query('users', 'u')
    ->where('u.status', '=', 'active')
    ->where('u.age', '>', '21')
    ->orderBy('u.created_at', 'DESC')
    ->get(); // Returns an EntityCollection

// Run high-performance aggregates directly on the server level
$averageAge = ModelEntity::query('users')->aggregate('AVG', 'age');
$totalRevenue = ModelEntity::query('orders')->aggregate('SUM', 'total_amount');

// Smart counting protects against broken counts when using GROUP BY
$count = ModelEntity::query('orders')
    ->groupBy('customer_id')
    ->count(); 


// Modifying object properties dynamically
$user = ModelEntity::query('users')->first();
$user->name = 'Jane Doe'; 

// The update routine safely merges and checks both manual changes and arguments 
// If nothing changed compared to the loading snapshot, no database query is executed.
$user->update(['status' => 'verified']); 

// Self-deletion capabilities
$user->delete();


$activeUsers = $users->filter(fn($user) => $user->status === 'active');

// Pluck a specific column as a scalar array
$emails = $users->pluck('email'); // ['[email protected]', '[email protected]']

// Look up a specific record loaded into memory by any key-value criterion
$targetUser = $users->find('id', 42);

if (!$users->isEmpty()) {
    $firstOne = $users->first();
}


use Eril\DBME\Database\DB;
use Eril\DBME\ModelEntity;

DB::transaction(function () {
    ModelEntity::table('accounts')->where('id', 1)->update(['balance' => 400]);
    ModelEntity::table('accounts')->where('id', 2)->update(['balance' => 800]);
    
    // If an error occurs here, both balance changes are completely rolled back.
});