PHP code example of xtompie / dao

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

    

xtompie / dao example snippets


/** @var Xtompie\Dao\Dao $dao */
$dao->insert('user', ['email' => '[email protected]', 'created_at' => time()]);
$records = $dao->query(['select' => '*', 'from' => 'user', 'limit' => 10]);

use PDO;
use Xtompie\Aql\Aql;
use Xtompie\Aql\PostgreSQLPlatform;
use Xtompie\Dao\Dao;

$dao = new Dao(
    adapter: new PdoAdapter(pdo: new PDO('pgsql:host=localhost;dbname=test', 'postgres')),
    aql: new Aql(platform: new PostgreSQLPlatform()),
);

class Dao
{
    public function query(array $query): array {}
    // returns all selected rows

    public function first(array $query): ?array {}
    // return first selected row or null

    public function val(array $query): mixed {}
    // return first field from first selected row or null

    public function any(array $query): bool {}
    // return true if there is any data for given query else false

    public function count(array $query): int {}
    // returns number of selected rows, by default uses `COUNT(*)`

    public function stream(array $query): Generator {}
    // yield records
}

class Dao
{
    public function records(string $table, ?array $where, ?string $order = null, ?int $offset = null, ?int $limit = null): array {}
    // similiar to `query`

    public function record(string $table, ?array $where = null, ?string $order = null, ?int $offset = null): ?array {}
    // similar to `first`

    public function amount(string $table, ?array $where = null, ?string $group = null): int {}
    // similiar to `count`

    public function exists(string $table, array $where): bool {}
    // similiar to `any`

    public function streamRecords(string $table, ?array $where = null, ?string $order = null, ?int $offset = null, ?int $limit = null): Generator
    // yield records

}

class Dao
{
    public function command(array $command): int {}

    public function insert(string $table, array $values): int {}

    public function insertBulk(string $table, array $bluk): int {}

    public function update(string $table, array $set, array $where): int {}

    public function upsert(string $table, array $set, array $where): int {}

    public function delete(string $table, array $where): int {}
}

class Dao
{
    public function transaction(callable $callback) {}
    // run callback in transaction
}


namespace App\Shared\Dao;

use Xtompie\Dao\Dao as BaseDao;

interface Paging
{
    public function limit(): int;
    public function offset(): int;
}

class Dao extends BaseDao
{
    public function aql(array $aql): array
    {
        if (isset($aql['paging'])) {
            $paging = $aql['paging'];
            unset($aql['paging']);
            if (!$paging instanceof Paging) {
                throw new \Exception();
            }
            $aql['offset'] => $paging->offset();
            $aql['limit'] => $paging->limit();
        }
        return parent::aql($aql);
    }
}