PHP code example of echo-fusion / databasemanager

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

    

echo-fusion / databasemanager example snippets


use EchoFusion\DatabaseManager\DatabaseManager;

$databases = DatabaseManager::fromConfig([
    'default' => 'main',
    'connections' => [
        'main' => ['driver' => 'mysql', 'host' => 'db', 'dbname' => 'app', 'user' => 'app', 'password' => 'secret'],
    ],
]);

$db = $databases->connection();                 // created on first use, then reused

$user = $db->fetchOne('SELECT * FROM users WHERE id = :id', ['id' => 7]);   // array|null
$rows = $db->fetchAll('SELECT * FROM users WHERE id IN (:ids)', ['ids' => [1, 2, 3]]);
$count = $db->fetchValue('SELECT COUNT(*) FROM users');
$changed = $db->execute('UPDATE users SET active = ? WHERE age < ?', [0, 18]);   // affected rows

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

$db->transactional(function ($db) {
    $db->execute('INSERT INTO log (msg) VALUES (?)', ['start']);

    try {
        $db->transactional(fn ($db) => $db->execute('INSERT INTO risky ...'));
    } catch (Throwable) {
        // "risky" was rolled back, "start" is still pending in the outer transaction
    }
});

use EchoFusion\DatabaseManager\Query\QueryBuilder;

$users = QueryBuilder::table($db, 'users')   // or $databases->table('users')
    ->select('id', 'name', 'created_at as joined')
    ->where('active', 1)
    ->where('age', '>=', 18)
    ->orWhere('role', 'admin')
    ->whereIn('country', ['IR', 'DE'])
    ->whereNull('deleted_at')
    ->orderBy('name')
    ->limit(20)
    ->offset(40)
    ->get();                                  // list of rows

$first = $query->first();                     // row or null
$name = $query->value('name');
$total = $query->count();
$exists = $query->exists();

$id = QueryBuilder::table($db, 'users')->insertGetId(['name' => 'Ann', 'age' => 30]);
QueryBuilder::table($db, 'users')->where('id', $id)->update(['age' => 31]);
QueryBuilder::table($db, 'users')->where('id', $id)->delete();

$query->toSql();                              // see the SQL
$query->getBindings();                        // and its parameters

return [
    'default' => 'main',
    'connections' => [
        // built in, PDO: mysql, pgsql or sqlite
        'main'  => ['driver' => 'mysql', 'host' => 'db', 'port' => 3306, 'dbname' => 'app',
                    'user' => 'app', 'password' => 'secret', 'charset' => 'utf8mb4'],
        'local' => ['driver' => 'sqlite', 'path' => __DIR__ . '/storage/app.sqlite'],   // or ':memory:'
        'pg'    => ['driver' => 'pgsql', 'host' => 'pg', 'dbname' => 'app', 'user' => 'app', 'password' => 'secret'],

        // replace the implementation: any class implementing ConnectionInterface,
        // created as `new YourClass($definition)` (without the "class" key)
        'orm'   => ['class' => DoctrineConnection::class, 'driver' => 'pdo_mysql', 'host' => 'db', 'dbname' => 'app',
                    'user' => 'app', 'password' => 'secret'],
    ],
];

use EchoFusion\DatabaseManager\Connections\DoctrineConnection;

$db = new DoctrineConnection(['driver' => 'pdo_mysql', 'host' => 'db', 'dbname' => 'app', 'user' => 'app', 'password' => 'secret']);
// or wrap a connection you already have: new DoctrineConnection($dbalConnection)

your code ──► ConnectionInterface (echo-fusion/contracts)
                     ▲
        ┌────────────┼──────────────────┐
  PdoConnection   DoctrineConnection   your own class
   (built in)      (optional adapter)
bash
composer