hgraca/micro-orm

该软件包已被废弃,不再维护。未建议替代软件包。

一个非常小的ORM库

dev-master / 1.0.x-dev 2016-12-16 16:14 UTC

This package is auto-updated.

Last update: 2023-07-30 19:16:49 UTC


README

Author Software License Latest Version Total Downloads

Build Status Coverage Status Quality Score

一个非常小的ORM库。它没有缓存或实例管理。我将其构建为一个学习工具,可能在某个时候它将是可用的,但始终作为一个非常薄的层。

用法

配置

配置可以像这样

[
    'repositoryFqcn' => MySqlPdoRepository::class,
    'dateTimeFormat' => 'Y-m-d H:i:s',
    'collectionFqcn' => Collection::class
    'dataMapper' => [
        Entity::class => [
            'entityFcqn'                 => Entity::class,
            // if not set, it will be inferred from the entity name,
            // if it doesnt exit, the default Repository will be used
            'repositoryFqcn'             => EntityRepository::class,
            'table'                      => ClassHelper::extractCanonicalClassName(Entity::class),
            'propertyToColumnNameMapper' => array_combine($properties, $properties),
            'collectionFqcn'             => Collection::class,
            'attributes' => [
                'id' => [ // by convention its always 'id'
                    'column' => 'id',
                    'type' => 'integer', // by convention its always an integer
                ],
                'aProperty' => [
                    'column' => 'aColumn_name',
                    'type' => 'integer', // integer, float, boolean, string, text, datetime
                ],
            ],
        ],
    ],
]

查询

对于简单查询,我们可以使用客户端的 select 方法,如下所示

$table = 'DummyTable';
$filter = [
    'propA' => true,
    'propB' => null,
    'propC' => ['filterC1' => 5, 'filterC2' => null],
];
$orderBy = [
    'propA' => 'ASC',
    'propB' => 'DESC',
];

$this->client->select($table, $filter, $orderBy);

上面的代码生成以下SQL

SELECT * 
FROM `DummyTable` 
WHERE 
    `propA`=:propA_filter 
    AND `propB` IS :propB_filter 
    AND (`propC`=:filterC1_filter OR `propC` IS :filterC2_filter) 
ORDER BY propA ASC, propB DESC

但对于更复杂的查询,我们应该自定义它们,并将它们传递给客户端的 executeQuery 方法,即

$sql = 'SELECT * 
        FROM `DummyTable` 
        WHERE 
            `propA`=:propA_filter 
            AND `propB` IS :propB_filter 
            AND (`propC`=:filterC1_filter OR `propC` IS :filterC2_filter) 
        ORDER BY propA ASC, propB DESC';
        
$filter = [
    'propA' => true,
    'propB' => null,
    'propC' => ['filterC1' => 5, 'filterC2' => null],
];

$this->executeQuery($sql, $filter);

约定

  • 所有实体都有一个ID,其属性名称为 'id',列名称为 'id',类型为 int
  • 默认要使用的数据库是第一个注册的

安装

要安装库,运行以下命令并将获得最新版本

composer require hgraca/micro-orm

测试

要运行测试,请运行

make test

或者以下任何一个

make test-acceptance
make test-functional
make test-integration
make test-unit
make test-humbug

要调试模式下运行测试,请运行

make test-debug

覆盖率

要生成测试覆盖率,请运行

make coverage

代码规范

要修复代码规范,请运行

make cs-fix

待办事项

  • 清理并测试存储库
  • 清理并测试数据映射器
  • 清理并测试配置
  • 文档说明如何使用存储库和查询类,以及如何不使用它们
  • 创建一个关系型配置格式,类似于Doctrine yml配置,但使用数组
  • 实现延迟加载
  • 创建一个实体管理器,管理器在请求结束时仅保存实体,并作为一级缓存工作
  • 实现二级缓存
  • 实现预加载