1. Go to this page and download the library: Download migears/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/ */
migears / dao example snippets
// Plain DAO — pdo + logger
class PlainUserDao
{
use SingleTableDao;
protected function table(): string { return 'users'; }
protected function idColumn(): string { return 'id'; }
protected function domainClass(): string { return UserDomain::class; }
public function __construct(PDO $pdo, LoggerInterface $logger)
{
$this->initDao($pdo, $logger);
}
}
// Cacheable DAO — pdo + logger + cache
class CachedUserDao
{
use CachedDao;
protected function table(): string { return 'users'; }
protected function idColumn(): string { return 'id'; }
protected function domainClass(): string { return UserDomain::class; }
public function __construct(PDO $pdo, LoggerInterface $logger, CacheInterface $cache)
{
$this->initDao($pdo, $logger);
$this->initCachedDao($cache, $logger);
}
}
use MiGears\Dao\SingleTableDao;
use MiGears\Domain\DataAccess;
class UserDomain
{
use DataAccess;
public function __construct(
public readonly int $id,
public readonly string $user_name,
public readonly string $email,
) {}
}
class UserDao
{
use SingleTableDao;
protected function table(): string { return 'users'; }
protected function idColumn(): string { return 'id'; }
protected function domainClass(): string { return UserDomain::class; }
public function __construct(PDO $pdo, LoggerInterface $logger)
{
$this->initDao($pdo, $logger);
}
}
class UserDao
{
use SingleTableDao;
use OffsetPagination;
protected function table(): string { return 'users'; }
protected function idColumn(): string { return 'id'; }
protected function domainClass(): string { return UserDomain::class; }
}
$page = $dao->paginate(2, 20); // runs one COUNT(*) for the total
$page->items(); // UserDomain[] on the current page
$page->total(); // int — rows across all pages
$page->totalPages(); // int
$page->hasNext(); // bool
class UserDao
{
use SingleTableDao;
use CursorPagination;
protected function table(): string { return 'users'; }
protected function idColumn(): string { return 'id'; }
protected function domainClass(): string { return UserDomain::class; }
}
$page = $dao->paginateAfter(null, 20); // first page
$page->items(); // UserDomain[]
$page->hasMore(); // bool
$page = $dao->paginateAfter($page->nextCursor(), 20); // next page
// Entity reads already return a Domain — no manual fromArray() step
$user = $dao->getByIdOrFail(1);
echo $user->user_name; // "Alice"
// save() takes a Domain and returns the persisted Domain (insert or update)
$user = $dao->save($userDomain);
// patch() writes only the named columns of a Domain that already holds the new
// values; every other column — including one changed concurrently — is untouched
$dao->patch($userDomain, ['user_name']);
// save() sees a non-empty key and updates nothing; insert() is the one path
// that persists a caller-assigned key.
$dao->insert(['uuid' => $uuid, 'user_name' => 'Alice']);
// The Domain already holds the new values (built by whatever flow produced it).
$affected = $dao->patch($user, ['user_name', 'email']);
// UPDATE `users` SET `user_name` = :set_user_name, `email` = :set_email WHERE `id` = …
// Only the named columns reach the SET clause: age, status and created_at keep
// whatever the row holds — even if another writer changed them after your read.
$affected; // 1 — rows changed (0 when the row no longer exists)
$user = $cachedDao->getById(1); // ?UserDomain (cached)
$user = $cachedDao->getByIdOrFail(1); // UserDomain (throws if not found)
$users = $cachedDao->getByIds([1, 2]); // [id => UserDomain] (cached)
$user = $cachedDao->save($userDomain); // Domain in, persisted Domain out
$all = $cachedDao->getAll(); // UserDomain[]
class UserDao
{
use CachedDao;
protected function table(): string { return 'users'; }
protected function idColumn(): string { return 'id'; }
protected function domainClass(): string { return UserDomain::class; }
public function __construct(PDO $pdo, LoggerInterface $logger, CacheInterface $cache)
{
$this->initDao($pdo, $logger);
$this->initCachedDao($cache, $logger);
}
protected function deleteCacheFor(?object $user): void
{
if ($user !== null) {
$this->cache->delete('user_by_email_' . $user->email);
}
}
}
class PostDao
{
use SingleTableDao;
protected function table(): string { return 'posts'; }
protected function idColumn(): string { return 'post_id'; }
protected function domainClass(): string { return PostDomain::class; }
public function __construct(PDO $pdo, LoggerInterface $logger)
{
$this->initDao($pdo, $logger);
}
}
// 普通 DAO — pdo + logger
class PlainUserDao
{
use SingleTableDao;
protected function table(): string { return 'users'; }
protected function idColumn(): string { return 'id'; }
protected function domainClass(): string { return UserDomain::class; }
public function __construct(PDO $pdo, LoggerInterface $logger)
{
$this->initDao($pdo, $logger);
}
}
// Cacheable DAO — pdo + logger + cache
class CachedUserDao
{
use CachedDao;
protected function table(): string { return 'users'; }
protected function idColumn(): string { return 'id'; }
protected function domainClass(): string { return UserDomain::class; }
public function __construct(PDO $pdo, LoggerInterface $logger, CacheInterface $cache)
{
$this->initDao($pdo, $logger);
$this->initCachedDao($cache, $logger);
}
}
use MiGears\Dao\SingleTableDao;
use MiGears\Domain\DataAccess;
class UserDomain
{
use DataAccess;
public function __construct(
public readonly int $id,
public readonly string $user_name,
public readonly string $email,
) {}
}
class UserDao
{
use SingleTableDao;
protected function table(): string { return 'users'; }
protected function idColumn(): string { return 'id'; }
protected function domainClass(): string { return UserDomain::class; }
public function __construct(PDO $pdo, LoggerInterface $logger)
{
$this->initDao($pdo, $logger);
}
}