1. Go to this page and download the library: Download ascetic-soft/rowcast 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/ */
ascetic-soft / rowcast example snippets
use AsceticSoft\Rowcast\Connection;
use AsceticSoft\Rowcast\DataMapper;
class UserDto
{
public int $id;
public string $email;
public bool $isActive;
}
$connection = Connection::create('sqlite::memory:');
$mapper = new DataMapper($connection);
$user = new UserDto();
$user->email = '[email protected]';
$user->isActive = true;
$mapper->insert('users', $user);
$found = $mapper->findOne(UserDto::class, ['email' => $user->email]);
use AsceticSoft\Rowcast\Connection;
// From DSN
$connection = Connection::create(
dsn: 'mysql:host=localhost;dbname=app',
username: 'root',
password: 'secret',
nestTransactions: true,
);
// From existing PDO
$pdo = new \PDO('sqlite::memory:');
$connection = new Connection($pdo, nestTransactions: true);
$stmt = $connection->executeQuery('SELECT * FROM users WHERE id = ?', [1]);
$affected = $connection->executeStatement('UPDATE users SET email = ? WHERE id = ?', ['[email protected]', 1]);
$rows = $connection->fetchAllAssociative('SELECT * FROM users');
$row = $connection->fetchAssociative('SELECT * FROM users WHERE id = ?', [1]);
$count = $connection->fetchOne('SELECT COUNT(*) FROM users');
$connection->onBeforeQuery(function (string $sql, array $params): void {
// log or inspect SQL before execution
});
$connection->onAfterQuery(function (string $sql, array $params, float $duration, ?\Throwable $exception): void {
// $exception is null for successful queries
});
use AsceticSoft\Rowcast\DataMapper;
use AsceticSoft\Rowcast\TypeConverter\TypeConverterInterface;
use AsceticSoft\Rowcast\TypeConverter\TypeConverterRegistry;
final class UuidConverter implements TypeConverterInterface
{
public function supports(string $phpType): bool
{
return $phpType === Uuid::class;
}
public function toPhp(mixed $value, string $phpType): mixed
{
return new Uuid((string) $value);
}
public function toDb(mixed $value): mixed
{
return (string) $value;
}
}
$converters = TypeConverterRegistry::defaults()->add(new UuidConverter());
$mapper = new DataMapper($connection, typeConverter: $converters);
use AsceticSoft\Rowcast\DataMapper;
use AsceticSoft\Rowcast\NameConverter\NameConverterInterface;
final class PrefixedConverter implements NameConverterInterface
{
public function toPropertyName(string $columnName): string
{
return lcfirst(str_replace('usr_', '', $columnName));
}
public function toColumnName(string $propertyName): string
{
return 'usr_' . $propertyName;
}
}
$mapper = new DataMapper($connection, nameConverter: new PrefixedConverter());
$rows = $connection->createQueryBuilder()
->select('*')
->from('users')
->where(['email' => '[email protected]', 'is_active' => 1])
->fetchAllAssociative();
// SQL: SELECT * FROM users WHERE email = :w_email AND is_active = :w_is_active
// IS NULL / IS NOT NULL
->where(['deleted_at' => null]) // deleted_at IS NULL
->where(['deleted_at !=' => null]) // deleted_at IS NOT NULL
// IN / NOT IN
->where(['status' => ['active', 'pending']]) // status IN (...)
->where(['status !=' => ['banned']]) // status NOT IN (...)
->where(['status IN' => ['active']]) // explicit IN
->where(['status NOT IN' => ['banned']]) // explicit NOT IN
// BackedEnum in WHERE (direct QueryBuilder usage)
->where(['status' => UserStatus::Active]) // status = :w_status, parameter value: 'active'
->where(['status' => [UserStatus::Active, UserStatus::Inactive]]) // status IN (...), parameters: 'active', 'inactive'
// Comparison operators
->where(['age >' => 18, 'age <=' => 65, 'score !=' => 0])
// LIKE / ILIKE / NOT LIKE / NOT ILIKE
->where(['name LIKE' => '%alice%'])
->where(['name ILIKE' => '%alice%']) // useful for PostgreSQL
->where(['name NOT LIKE' => '%bot%'])
->where(['name NOT ILIKE' => '%bot%']) // PostgreSQL only
// BETWEEN
->where(['age BETWEEN' => [18, 65]])
// (status = 'active' AND age > 18) OR (role = 'admin')
$rows = $connection->createQueryBuilder()
->select('*')
->from('users')
->whereOr(
['status' => 'active', 'age >' => 18],
['role' => 'admin'],
)
->fetchAllAssociative();
// deleted_at IS NULL AND ((status = 'active') OR (role = 'admin'))
$rows = $connection->createQueryBuilder()
->select('*')
->from('users')
->where(['deleted_at' => null])
->andWhereOr(['status' => 'active'], ['role' => 'admin'])
->fetchAllAssociative();