1. Go to this page and download the library: Download ray/aura-sql-module 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/ */
ray / aura-sql-module example snippets
use Ray\Di\AbstractModule;
use Ray\AuraSqlModule\AuraSqlModule;
use Ray\AuraSqlModule\AuraSqlQueryModule;
class AppModule extends AbstractModule
{
protected function configure()
{
$this->install(
new AuraSqlModule(
'mysql:host=localhost;dbname=test',
'username',
'password',
'slave1,slave2,slave3', // optional slave server list
$options, // optional key=>value array of driver-specific connection options
$queries // Queries to execute after the connection.
)
);
}
}
$this->install(
new AuraSqlEnvModule(
'PDO_DSN', // getenv('PDO_DSN')
'PDO_USER', // getenv('PDO_USER')
'PDO_PASSWORD', // getenv('PDO_PASSWORD')
'PDO_SLAVE', // getenv('PDO_SLAVE')
$options, // optional key=>value array of driver-specific connection options
$queries // Queries to execute after the connection.
)
);
use Ray\Di\AbstractModule;
use Ray\AuraSqlModule\AuraSqlModule;
use Aura\Sql\ConnectionLocator;
use Aura\Sql\Connection;
class AppModule extends AbstractModule
{
protected function configure()
{
$locator = new ConnectionLocator;
$locator->setWrite('master', new Connection('mysql:host=localhost;dbname=master', 'id', 'pass'));
$locator->setRead('slave1', new Connection('mysql:host=localhost;dbname=slave1', 'id', 'pass'));
$locator->setRead('slave2', new Connection('mysql:host=localhost;dbname=slave2', 'id', 'pass'));
$this->install(new AuraSqlReplicationModule($locator));
}
}
#[Inject]
public function setLoggerDb(#[Named('log_db')] ExtendedPdoInterface $pdo)
{
// ...
}
class AppModule extends AbstractModule
{
protected function configure()
{
$this->install(new NamedPdoModule('log_db', 'mysql:host=localhost;dbname=log', 'username', 'password'));
}
}
class AppModule extends AbstractModule
{
protected function configure()
{
$this->install(new NamedPdoEnvModule('log_db', 'LOG_DSN', 'LOG_USERNAME', 'LOG_PASSWORD'));
}
}
class AppModule extends AbstractModule
{
protected function configure()
{
$this->install(new AuraSqlReplicationModule($locator, 'log_db'));
}
}
use Ray\AuraSqlModule\Annotation\WriteConnection; // important
use Ray\AuraSqlModule\Annotation\Transactional; // important
class User
{
public $pdo;
#[WriteConnection, Transactional]
public function write()
{
// $this->pdo->rollback(); when exception thrown.
}
}
use Aura\SqlQuery\Common\SelectInterface;
use Aura\Sql\ExtendedPdoInterface;
class UserRepository
{
public function __construct(
private readonly SelectInterface $select,
private readonly ExtendedPdoInterface $pdo
) {}
public function findById(int $id): array
{
$statement = $this->select
->distinct() // SELECT DISTINCT
->cols([ // select these columns
'id', // column name
'name AS namecol', // one way of aliasing
'col_name' => 'col_alias', // another way of aliasing
'COUNT(foo) AS foo_count' // embed calculations directly
])
->from('users AS u') // FROM these tables
->where('id = :id')
->getStatement();
return $this->pdo->fetchAssoc($statement, ['id' => $id]);
}
}
use Aura\SqlQuery\Common\SelectInterface;
use Aura\SqlQuery\Common\InsertInterface;
use Aura\SqlQuery\Common\UpdateInterface;
use Aura\Sql\ExtendedPdoInterface;
class UserService
{
public function __construct(
private readonly SelectInterface $select,
private readonly InsertInterface $insert,
private readonly UpdateInterface $update,
private readonly ExtendedPdoInterface $pdo
) {}
public function createUser(array $userData): int
{
$statement = $this->insert
->into('users')
->cols($userData)
->getStatement();
$this->pdo->perform($statement, $this->insert->getBindValues());
return (int) $this->pdo->lastInsertId();
}
public function updateUser(int $id, array $userData): bool
{
$statement = $this->update
->table('users')
->cols($userData)
->where('id = :id')
->bindValue('id', $id)
->getStatement();
return $this->pdo->perform($statement, $this->update->getBindValues());
}
}
use Ray\AuraSqlModule\Pagerfanta\AuraSqlPagerFactoryInterface;
use Aura\Sql\ExtendedPdoInterface;
class UserListService
{
public function __construct(
private readonly AuraSqlPagerFactoryInterface $pagerFactory,
private readonly ExtendedPdoInterface $pdo
) {}
public function getUserList(int $page): Page
{
$sql = 'SELECT * FROM users WHERE active = :active';
$params = ['active' => 1];
$pager = $this->pagerFactory->newInstance($this->pdo, $sql, $params, 10, '/?page={page}&category=users');
return $pager[$page];
}
}
use Ray\AuraSqlModule\Pagerfanta\AuraSqlQueryPagerFactoryInterface;
use Aura\SqlQuery\Common\SelectInterface;
use Aura\Sql\ExtendedPdoInterface;
class ProductListService
{
public function __construct(
private readonly AuraSqlQueryPagerFactoryInterface $queryPagerFactory,
private readonly SelectInterface $select,
private readonly ExtendedPdoInterface $pdo
) {}
public function getProductList(int $page, string $category): Page
{
$select = $this->select
->from('products')
->where('category = :category')
->bindValue('category', $category);
$pager = $this->queryPagerFactory->newInstance($this->pdo, $select, 10, '/?page={page}&category=' . $category);
return $pager[$page];
}
}
use Pagerfanta\View\Template\TemplateInterface;
use Pagerfanta\View\Template\TwitterBootstrap3Template;
use Ray\AuraSqlModule\Annotation\PagerViewOption;
$this->bind(TemplateInterface::class)->to(TwitterBootstrap3Template::class);
$this->bind()->annotatedWith(PagerViewOption::class)->toInstance($pagerViewOption);
class DevModule extends AbstractModule
{
protected function configure()
{
// ...
$this->install(new AuraSqlProfileModule());
$this->bind(LoggerInterface::class)->toInstance(
new class extends AbstractLogger {
/** @inheritDoc */
public function log($level, $message, array $context = [])
{
$replace = [];
foreach ($context as $key => $val) {
if (! is_array($val) && (! is_object($val) || method_exists($val, '__toString'))) {
$replace['{' . $key . '}'] = $val;
}
}
error_log(strtr($message, $replace));
}
}
);
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.