PHP code example of patrick-hanna / database-crud-buffer

1. Go to this page and download the library: Download patrick-hanna/database-crud-buffer 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/ */

    

patrick-hanna / database-crud-buffer example snippets


use PatrickHanna\DatabaseCrudBuffer\DatabaseCrudBuffer;

$buffer = new DatabaseCrudBuffer();

$buffer->insert('accounts', ['id' => '1', 'legal_name' => 'Acme']);
$buffer->insert(Account::class, ['id' => '2', 'legal_name' => 'Globex']);

$buffer->flush();

$buffer = new DatabaseCrudBuffer(
    maxSize: 10000,        // null to disable auto-flush
    useTransaction: false, // flush without opening a transaction
);

$buffer->insert('accounts', ['id' => '1', 'name' => 'Acme']);

$buffer->upsert(
    tableOrModelClass: 'accounts',
    uniqueBy: ['id'],
    row: ['id' => '1', 'name' => 'New'],
    updateColumns: ['name'],          // optional; defaults to the buffered columns
    originalValues: ['id' => '1', 'name' => 'Old', 'email' => '[email protected]'],
);

$buffer->update('accounts', 'id', 'account-1', ['name' => 'Acme']);
$buffer->update('accounts', ['status', 'active'], ['email' => '[email protected]']);
$buffer->update('accounts', [['status', 'active'], ['id', 'a']], ['name' => 'Acme']);
$buffer->update('accounts', ['id', 'in', ['a', 'b']], ['status' => 'active']);

$buffer->delete('accounts', 'id', 'account-1');
$buffer->delete('accounts', [['status', 'active'], ['id', 'a']]);
$buffer->delete('accounts', ['id', 'in', ['a', 'b']]);

$buffer->flush();                   // wraps the work in DB::transaction(...)
$buffer->flushWithoutTransaction(); // caller owns the transaction boundary