toutaphp/ogam-data-mapper
Composer 安装命令:
composer require toutaphp/ogam-data-mapper
包简介
A MyBatis-inspired SQL mapping framework for PHP. Write the SQL you want, map results to objects automatically.
README 文档
README
Ogam (ᚑᚌᚐᚋ) is a MyBatis-inspired SQL mapping framework for PHP. Write the SQL you want, map results to objects automatically.
Ogam is the ancient Celtic alphabet used by Druids to inscribe sacred knowledge. Like Ogam mapped symbols to meaning, this library maps SQL results to PHP objects.
Features
- SQL-First: Write exactly the SQL you want, optimized for your use case
- Declarative Mapping: Configure how results become objects via XML or attributes
- Dynamic SQL: Build queries conditionally with
<if>,<foreach>,<where>,<set> - Type Handlers: Automatic conversion between PHP and SQL types (DateTime, Enums, JSON)
- Zero Magic: Every SQL executed is visible and predictable
- Framework Agnostic: Works standalone or with Symfony/Laravel
Requirements
- PHP 8.3+
- PDO extension
- MySQL, PostgreSQL, or SQLite
Installation
composer require toutaphp/ogam-data-mapper
Quick Start
1. Define Your Entity
<?php namespace App\Entities; class User { public function __construct( public int $id, public string $name, public string $email, public DateTimeImmutable|null $email_verified_at, public string $password, public string|null $remember_token, public datetime $created_at, public datetime $updated_at, ) {} }
2. Data mapper configuration
<!-- config/ogam.xml --> <?xml version="1.0" encoding="UTF-8"?> <configuration> <typeAliases> <typeAlias alias="User" type="App\Entity\User"/> </typeAliases> <typeHandlers> <typeHandler phpType="App\Enum\Status" handler="App\Handlers\StatusTypeHandler"/> </typeHandlers> <environments default="development"> <environment id="development"> <dataSource type="POOLED"> <property name="driver" value="mysql"/> <property name="host" value=""/> <property name="port" value=""/> <property name="dbname" value=""/> <property name="username" value=""/> <property name="password" value=""/> </dataSource> </environment> </environments> <mappers> <mapper resource="../database/mappers/UserMapper.xml"/> </mappers> </configuration>
3. Create XML Mapper
<!-- mappers/UserMapper.xml --> <?xml version="1.0" encoding="UTF-8"?> <mapper namespace="App\Mappers\UserMapper"> <select id="findById" resultType="App\Entity\User"> SELECT id, name, email, email_verified_at, password, remember_token, created_at, updated_at FROM users WHERE id = #{id} </select> <select id="findByStatus" resultType="App\Entity\User"> SELECT id, name, email, email_verified_at, password, remember_token, created_at, updated_at FROM users <where> <if test="status != null"> AND status = #{status} </if> </where> </select> <insert id="insert" useGeneratedKeys="true" keyProperty="id"> INSERT INTO users (name, email, email_verified_at, password, remember_token, created_at, updated_at) VALUES (#{name}, #{email}, #{emailVerifiedAt}, #{password}, #{rememberToken}, #{createdAt}, #{updatedAt}) </insert> </mapper>
4. Define Mapper Interface
<?php namespace App\Mappers; use Touta\Ogam\Attribute\Mapper; use App\Entities\User; #[Mapper(resource: 'database/mappers/UserMapper.xml')] interface UserMapper { public function findById(int $id): ?User; public function findByStatus(?Status $status): array; public function insert(User $user): int; }
5. Use the Mapper
<?php use Touta\Ogam\SessionFactoryBuilder; // Build session factory $factory = (new SessionFactoryBuilder()) ->withConfiguration(base_path('config/ogam.xml')) ->build(); // Open session $session = $factory->openSession(); // Get mapper $userMapper = $session->getMapper(UserMapper::class); // Query $user = $userMapper->findById(1); // Insert $newUser = new User(0, 'john', 'john@example.com', Status::ACTIVE, new DateTimeImmutable()); $userMapper->insert($newUser); echo $newUser->id; // Generated ID // Commit and close $session->commit(); $session->close();
Documentation
Parameter Syntax
Ogam uses MyBatis-style parameter syntax:
<!-- Value binding (safe, parameterized) --> SELECT * FROM users WHERE id = #{id} AND status = #{status} <!-- Identifier substitution (for column/table names) --> SELECT * FROM users ORDER BY ${orderColumn} ${orderDir}
Dynamic SQL
<select id="search" resultType="User"> SELECT * FROM users <where> <if test="name != null"> AND name LIKE #{name} </if> <if test="email != null"> AND email = #{email} </if> <if test="statuses != null"> AND status IN <foreach collection="statuses" item="s" open="(" separator="," close=")"> #{s} </foreach> </if> </where> </select>
Why Ogam?
| vs Doctrine | Ogam Advantage |
|---|---|
| Hydration overhead | Constructor injection, no reflection |
| Hidden SQL | Full SQL visibility and control |
| Learning curve | Write SQL you know, not DQL |
| vs Eloquent | Ogam Advantage |
|---|---|
| Performance | 2-3x faster (no Active Record overhead) |
| SQL control | Write optimized SQL directly |
| Testability | Clean separation, no database for unit tests |
Part of Toutā Framework
Ogam is part of the Toutā Framework ecosystem:
- Toutā: Core framework
- Cosan: HTTP router
- Fíth: Template engine
- Nasc: Dependency injection
- Ogam: Data mapping (this library)
Each component works standalone or together.
Contributing
See CONTRIBUTING.md for guidelines.
License
MIT License. See LICENSE for details.
toutaphp/ogam-data-mapper 适用场景与选型建议
toutaphp/ogam-data-mapper 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 0 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 01 月 20 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「database」 「orm」 「sql」 「pdo」 「mapper」 「mybatis」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 toutaphp/ogam-data-mapper 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 toutaphp/ogam-data-mapper 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 toutaphp/ogam-data-mapper 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Dibi is Database Abstraction Library for PHP
Kinikit - PHP Application development framework MVC component
Store your language lines in the database, yaml or other sources
PHP Database ORM for Symfony1. Do NOT use for new projects: please move to a newest Symfony release and Doctrine2
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
Query filtering in your frontend
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 28
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-01-20