PHP code example of sobhanmohammadi / arxaron

1. Go to this page and download the library: Download sobhanmohammadi/arxaron 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/ */

    

sobhanmohammadi / arxaron example snippets


use Arxaron\Config;
use Arxaron\Database;

Database::addConnection(Config::fromArray([
    'host'     => '127.0.0.1',
    'username' => 'app',
    'password' => getenv('DB_PASSWORD'),
    'database' => 'shop',
    'charset'  => 'utf8mb4',
]));

// Optional: enable Redis-backed query caching (uses predis/predis).
Database::useRedisCache(new Predis\Client([
    'scheme' => 'tcp',
    'host'   => '127.0.0.1',
    'port'   => 6379,
]));

// Fluent query builder
$activeUsers = Database::table('users')
    ->select('id', 'name', 'email')
    ->where('active', '=', 1)
    ->orderBy('created_at', 'DESC')
    ->limit(20)
    ->get();

foreach ($activeUsers as $row) {
    echo $row['name'], "\n";
}

// Raw parameterized SQL (positional or named placeholders)
$row = Database::query('SELECT * FROM users WHERE email = :email', [
    'email' => '[email protected]',
])->first();

Database::table('orders')
    ->select('orders.id', 'orders.total', 'customers.name')
    ->join('customers', 'orders.customer_id', '=', 'customers.id')
    ->where('orders.status', '=', 'paid')
    ->whereBetween('orders.created_at', $start, $end)
    ->whereIn('orders.region', ['EU', 'UK'])
    ->orWhere('orders.priority', '=', 'high')
    ->groupBy('customers.id')
    ->having('SUM(orders.total)', '>', 1000)
    ->orderBy('orders.total', 'DESC')
    ->paginate(page: 2, perPage: 25)
    ->get();

Database::table('products')
    ->where('active', '=', 1)
    ->whereGroup(function ($q) {
        $q->where('price', '<', 10)->orWhere('on_sale', '=', 1);
    })
    ->get();

$id = Database::table('users')->insert([
    'name'  => 'Ada Lovelace',
    'email' => '[email protected]',
]);

Database::table('users')->insertMany([
    ['name' => 'Grace', 'email' => '[email protected]'],
    ['name' => 'Alan',  'email' => '[email protected]'],
]);

Database::table('users')->where('id', '=', $id)->update(['active' => 0]);
Database::table('users')->where('id', '=', $id)->delete();

Database::transaction(function ($conn) {
    $conn->execute('UPDATE accounts SET balance = balance - ? WHERE id = ?', [100, 1]);
    $conn->execute('UPDATE accounts SET balance = balance + ? WHERE id = ?', [100, 2]);
});

$topProducts = Database::table('products')
    ->where('active', '=', 1)
    ->orderBy('sales', 'DESC')
    ->limit(10)
    ->cache(ttlSeconds: 60) // cached under a key tagged with "products"
    ->get();

// Any insert/update/delete on `products` automatically invalidates
// everything cached under that tag.

use Arxaron\Model;

final class User extends Model
{
    protected static string $table = 'users';
    protected static array $fillable = ['name', 'email'];
    protected static ?int $cacheTtl = 30;
}

$user = User::create(['name' => 'Ada', 'email' => '[email protected]']);
$user = User::find(1);
$user->name = 'Ada Lovelace';
$user->save();
$user->delete();

foreach (User::query()->where('active', '=', 1)->get() as $row) {
    // ...
}

foreach (Database::connection()->cursor('SELECT * FROM events') as $row) {
    // processed one row at a time, not buffered in memory
}

Database::addConnection(Config::fromArray([...]), name: 'analytics');
Database::table('events', connection: 'analytics')->get();