承接 ascetic-soft/rowcast 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

ascetic-soft/rowcast

Composer 安装命令:

composer require ascetic-soft/rowcast

包简介

Lightweight DataMapper and QueryBuilder over PDO with DTO hydration and type casting

README 文档

README

CI codecov PHPStan Level 9 Latest Stable Version Total Downloads PHP Version License

Lightweight DataMapper over PDO for PHP 8.4+.

Rowcast maps database rows to DTOs and back using reflection, supports explicit/auto mapping, type conversion, and includes a fluent query builder with dialect-aware UPSERT.

Documentation: English | Русский | LLM/Context7

Requirements

  • PHP >= 8.4
  • ext-pdo

Installation

composer require ascetic-soft/rowcast

Quick Start

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 = 'alice@example.com';
$user->isActive = true;

$mapper->insert('users', $user);
$found = $mapper->findOne(UserDto::class, ['email' => $user->email]);

Core Concepts

Rowcast supports two mapping styles:

  • Auto mapping — pass class-string for reads and table name for writes. Names are converted via NameConverterInterface (default: SnakeCaseToCamelCase).
  • Explicit mapping — pass Mapping to control table name, column/property pairs, and ignored properties.

Auto Mapping

Table name is derived from DTO class name for reads:

Class Table
User users
UserProfile user_profiles

Column/property conversion (default):

Column Property
created_at createdAt
is_active isActive

Explicit Mapping

use AsceticSoft\Rowcast\Mapping;

$mapping = Mapping::auto(UserDto::class, 'custom_users')
    ->column('usr_email', 'email')
    ->ignore('internalNote');

$user = $mapper->findOne($mapping, ['id' => 1]);

Use Mapping::explicit(...) when only declared columns must be used:

$mapping = Mapping::explicit(UserDto::class, 'custom_users')
    ->column('id', 'id')
    ->column('usr_email', 'email');

Notes:

  • Mapping is currently mutable and configured in place.
  • Prefer building it once during setup and then reusing it as configuration.
  • Avoid mutating the same Mapping instance after it is already shared across multiple operations or services.

Connection

Connection wraps PDO and provides query helpers, transaction API, nested transaction support (savepoints), and query-builder factory.

Create Connection

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);

Raw Queries

$stmt = $connection->executeQuery('SELECT * FROM users WHERE id = ?', [1]);
$affected = $connection->executeStatement('UPDATE users SET email = ? WHERE id = ?', ['a@x.com', 1]);
$rows = $connection->fetchAllAssociative('SELECT * FROM users');
$row = $connection->fetchAssociative('SELECT * FROM users WHERE id = ?', [1]);
$count = $connection->fetchOne('SELECT COUNT(*) FROM users');

Query Events

Register lightweight listeners to observe SQL execution.

$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
});

Transactions

$connection->transactional(function (Connection $conn) {
    $conn->executeStatement('INSERT INTO users (email) VALUES (?)', ['alice@example.com']);
});

Nested mode creates savepoints for inner transactions.

DataMapper API

Main methods:

  • insert(string|Mapping $target, object $dto): void
  • batchInsert(string|Mapping $target, array $dtos, ?int $maxBindParameters = null): void
  • update(string|Mapping $target, object $dto, array $where): int
  • batchUpdate(string|Mapping $target, array $dtos, array $identityProperties, ?int $maxBindParameters = null): void
  • delete(string|Mapping $target, array $where): int
  • findAll(string|Mapping $target, array $where = [], array $orderBy = [], ?int $limit = null, ?int $offset = null): array
  • iterateAll(string|Mapping $target, array $where = [], array $orderBy = [], ?int $limit = null, ?int $offset = null): iterable
  • findOne(string|Mapping $target, array $where = []): ?object
  • save(string|Mapping $target, object $dto, string ...$identityProperties): void
  • upsert(string|Mapping $target, object $dto, string ...$conflictProperties): int
  • batchUpsert(string|Mapping $target, array $dtos, array $conflictProperties, ?int $maxBindParameters = null): void
  • hydrate(...), hydrateAll(...), extract(...)

CRUD Example

$dto = new UserDto();
$dto->email = 'alice@example.com';
$dto->isActive = true;

$mapper->insert('users', $dto);
$one = $mapper->findOne(UserDto::class, ['email' => $dto->email]);

$one->isActive = false;
$mapper->update('users', $one, ['id' => $one->id]);
$mapper->delete('users', ['id' => $one->id]);

save(...) Example

save(...) checks row existence by identity columns, then performs insert or update.

Notes:

  • save(...) is a convenience API and does a read-before-write flow.
  • It does not require a database conflict constraint to exist.
  • It may use two SQL statements for one logical save operation.
  • Prefer upsert(...) when your database and schema support conflict handling and write-path efficiency matters.
$mapper->save('users', $dto, 'id');

upsert(...) Example

$affected = $mapper->upsert('users', $dto, 'email');

Batch operations

$mapper->batchInsert('users', [$dto1, $dto2, $dto3]);
$mapper->batchUpsert('users', [$dto1, $dto2, $dto3], ['id']);
$mapper->batchUpdate('users', [$dto1, $dto2, $dto3], ['id']);

// Optional override for chunk sizing by bind parameter limit
$mapper->batchInsert('users', [$dto1, $dto2, $dto3], 500);
$mapper->batchUpsert('users', [$dto1, $dto2, $dto3], ['id'], 500);

batchInsert and batchUpsert automatically split large input into chunks based on DB parameter limits (for example, SQLite: 999 bind params), while executing all chunks inside one transaction.

Advanced where in DataMapper

DataMapper passes where arrays to the same QueryBuilder condition engine, so advanced operators are available there as well:

$users = $mapper->findAll(UserDto::class, where: [
    'deleted_at' => null,
    '$or' => [
        ['status' => ['active', 'pending']],
        ['role' => 'admin'],
    ],
    'age >=' => 18,
]);

Type Conversion

Rowcast converts DB values to declared PHP property types on hydrate, and PHP values to DB-safe values on extract/write.

Built-in converters:

  • ScalarConverter (int, float, string)
  • BoolConverter (bool <-> 0/1)
  • DateTimeConverter (DateTimeInterface <-> formatted UTC string)
  • JsonConverter (array <-> JSON)
  • EnumConverter (BackedEnum <-> backing value)

Custom Type Converter

Implement TypeConverterInterface and pass a custom registry to DataMapper:

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);

Custom Name Converter

Implement NameConverterInterface and pass it to DataMapper:

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());

Query Builder

Connection::createQueryBuilder() provides a fluent SQL builder.

SELECT

$rows = $connection->createQueryBuilder()
    ->select('u.id', 'u.email')
    ->from('users', 'u')
    ->where('u.is_active = :active')
    ->orderBy('u.id', 'DESC')
    ->setOffset(20)
    ->setLimit(10)
    ->setParameter('active', 1)
    ->fetchAllAssociative();

For pagination, use:

  • setOffset(int $offset) — start row
  • setLimit(int $limit) — max rows

You can also pass associative arrays to where(), andWhere(), and orWhere():

$rows = $connection->createQueryBuilder()
    ->select('*')
    ->from('users')
    ->where(['email' => 'alice@example.com', 'is_active' => 1])
    ->fetchAllAssociative();
// SQL: SELECT * FROM users WHERE email = :w_email AND is_active = :w_is_active

array predicates are converted to field = :param expressions joined by AND:

  • where(['a' => 1, 'b' => 2]) -> a = :w_a AND b = :w_b
  • andWhere(['a' => 1]) appends another AND block
  • orWhere(['a' => 1]) wraps previous predicate: (prev OR a = :w_a)

Parameter names are generated automatically and made unique (:w_id, :w_id_1, ...).

Supported array operators:

// 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]])

Operator reference:

Input Example SQL fragment (shape)
Equality ['id' => 10] id = :w_id
IS NULL ['deleted_at' => null] deleted_at IS NULL
IS NOT NULL ['deleted_at !=' => null] deleted_at IS NOT NULL
IN (auto) ['status' => ['active', 'pending']] status IN (:w_status, :w_status_1)
NOT IN (auto) ['status !=' => ['banned']] status NOT IN (:w_status, ...)
IN (explicit) ['status IN' => ['active']] status IN (:w_status)
NOT IN (explicit) ['status NOT IN' => ['banned']] status NOT IN (:w_status)
Comparison ['age >=' => 18] age >= :w_age
LIKE ['name LIKE' => 'A%'] name LIKE :w_name
ILIKE ['name ILIKE' => 'a%'] name ILIKE :w_name
NOT LIKE ['name NOT LIKE' => '%bot%'] name NOT LIKE :w_name
NOT ILIKE ['name NOT ILIKE' => '%bot%'] name NOT ILIKE :w_name
BETWEEN ['age BETWEEN' => [18, 65]] age BETWEEN :w_age AND :w_age_1

Notes:

  • Empty IN array compiles to 1 = 0 (always false).
  • Empty NOT IN array compiles to 1 = 1 (always true).
  • ILIKE and NOT ILIKE are PostgreSQL-specific.
  • BackedEnum values are normalized to backing scalar values in WHERE parameters (including IN / NOT IN arrays).

Dialect-specific operator support:

Dialect Extra operators over base set
PostgreSQL (pgsql) ILIKE, NOT ILIKE
MySQL (mysql) none
SQLite (sqlite) none
Generic/other drivers none

Base set for all dialects: >, >=, <, <=, LIKE, NOT LIKE.

OR Conditions

You can compose OR logic in two ways.

Method-based OR groups:

// (status = 'active' AND age > 18) OR (role = 'admin')
$rows = $connection->createQueryBuilder()
    ->select('*')
    ->from('users')
    ->whereOr(
        ['status' => 'active', 'age >' => 18],
        ['role' => 'admin'],
    )
    ->fetchAllAssociative();

Combine with existing filters:

// 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();

Nested-key style in one array:

$rows = $connection->createQueryBuilder()
    ->select('*')
    ->from('users')
    ->where([
        'age >' => 18,
        '$or' => [
            ['status' => 'active'],
            ['$and' => [
                ['role' => 'admin'],
                ['verified' => true],
            ]],
        ],
    ])
    ->fetchAllAssociative();

OR composition reference:

Pattern Example SQL fragment (shape)
whereOr(...groups) ->whereOr(['status' => 'active'], ['role' => 'admin']) ((status = :w_status) OR (role = :w_role))
andWhereOr(...groups) ->where(['deleted_at' => null])->andWhereOr(['status' => 'active'], ['role' => 'admin']) deleted_at IS NULL AND ((status = :w_status) OR (role = :w_role))
$or inside where([...]) ->where(['age >' => 18, '$or' => [['status' => 'active'], ['role' => 'admin']]]) age > :w_age AND ((status = :w_status) OR (role = :w_role))
$and inside $or ->where(['$or' => [['status' => 'active'], ['$and' => [['role' => 'admin'], ['verified' => true]]]]]) ((status = :w_status) OR ((role = :w_role) AND (verified = :w_verified)))
Mixed operators in OR groups ->whereOr(['status' => ['active', 'pending'], 'deleted_at' => null], ['name LIKE' => 'A%', 'age BETWEEN' => [18, 65]]) ((status IN (...) AND deleted_at IS NULL) OR (name LIKE :w_name AND age BETWEEN :w_age AND :w_age_1))

INSERT / UPDATE / DELETE

$connection->createQueryBuilder()
    ->insert('users')
    ->values(['email' => ':email', 'is_active' => ':is_active'])
    ->setParameter('email', 'alice@example.com')
    ->setParameter('is_active', 1)
    ->executeStatement();

$connection->createQueryBuilder()
    ->update('users')
    ->values(['is_active' => ':is_active'])
    ->where('id = :id')
    ->setParameter('is_active', 0)
    ->setParameter('id', 1)
    ->executeStatement();

$connection->createQueryBuilder()
    ->delete('users')
    ->where('id = :id')
    ->setParameter('id', 1)
    ->executeStatement();

UPSERT

$sql = $connection->createQueryBuilder()
    ->upsert('users')
    ->values(['email' => ':email', 'name' => ':name'])
    ->onConflict('email')
    ->doUpdateSet(['name'])
    ->getSQL();

Upsert is compiled via SQL dialects:

  • MysqlDialect
  • PostgresDialect
  • SqliteDialect
  • GenericDialect (throws for unsupported UPSERT)

WHERE array operator support is also dialect-aware (for example, ILIKE/NOT ILIKE only for PostgreSQL).

Expressions in SET / VALUES

Use Expression::raw() to embed arbitrary SQL expressions into UPDATE SET or INSERT VALUES:

use AsceticSoft\Rowcast\QueryBuilder\Expression\Expression;

// Decrement balance: balance = balance - :amount
$qb = $connection->createQueryBuilder()
    ->update('credit_accounts')
    ->set('balance', Expression::raw('balance - :amount'))
    ->where(['id' => 1])
    ->setParameter('amount', 100)
    ->executeStatement();

// Using NOW() in INSERT
$connection->createQueryBuilder()
    ->insert('logs')
    ->values([
        'message'    => ':msg',
        'created_at' => Expression::raw('NOW()'),
    ])
    ->setParameter('msg', 'user login')
    ->executeStatement();

// Column references in expressions
$connection->createQueryBuilder()
    ->update('items')
    ->set('quantity', Expression::raw('quantity + :add_qty'))
    ->where(['id' => 5])
    ->setParameter('add_qty', 3)
    ->executeStatement();

Expression SQL is written through verbatim — no parameter binding or quoting happens on the expression itself. You pass placeholders (:param) as part of the raw expression and bind them separately via setParameter().

Architecture

AsceticSoft\Rowcast\
├── ConnectionInterface
├── Connection
├── DataMapper
├── Hydrator
├── Extractor
├── Mapping
├── TargetResolver
├── QueryHelper
├── NameConverter\
│   ├── NameConverterInterface
│   └── SnakeCaseToCamelCase
├── TypeConverter\
│   ├── TypeConverterInterface
│   ├── TypeConverterRegistry
│   ├── ScalarConverter
│   ├── BoolConverter
│   ├── DateTimeConverter
│   ├── JsonConverter
│   └── EnumConverter
└── QueryBuilder\
    ├── QueryBuilder
    ├── QueryType
    ├── Dialect\
    │   ├── DialectInterface
    │   ├── DialectFactory
    │   ├── AbstractStandardDialect
    │   ├── AbstractOnConflictDialect
    │   ├── MysqlDialect
    │   ├── PostgresDialect
    │   ├── SqliteDialect
    │   └── GenericDialect
    └── Compiler\
        ├── SqlCompilerInterface
        ├── SqlFragments
        ├── SelectCompiler
        ├── InsertCompiler
        ├── UpsertCompiler
        ├── UpdateCompiler
        └── DeleteCompiler

Development

composer install
make install-hooks
vendor/bin/phpunit
vendor/bin/phpstan analyse

License

MIT

ascetic-soft/rowcast 适用场景与选型建议

ascetic-soft/rowcast 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 75 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 02 月 11 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「database」 「orm」 「php」 「pdo」 「datamapper」 「mapping」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 ascetic-soft/rowcast 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 ascetic-soft/rowcast 我们能提供哪些服务?
定制开发 / 二次开发

基于 ascetic-soft/rowcast 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 75
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 34
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-02-11