neoan3-apps/transformer

利用 neoan3 模型转换器进行模型抽象

v1.1.4 2021-04-21 03:01 UTC

README

它做什么

此应用程序处理 neoan3 转换器和基于魔法方法处理的自动 CRUD 操作。

安装

composer require neoan3-apps/transformer

准备

您需要以下设置

模型

一个模型(model/some/Some.model.php)可以看起来像这样

<?php

namespace Neoan3\Model;

use Neoan3\Apps\Transformer;

class SomeModel extends IndexModel
{
    static function __callStatic($method, $arguments)
    {
        $handOff = [$method, $arguments];
        return Transformer::addMagic(...$handOff);
    }
    static function test($some){
        var_dump('If function exists in model, it will be executed');
    }

}

addMagic($method, $arguments, $customTransformerClass = false, $assumesUniqueIdsInDb = true, $customPathForMigrationJSON = false)

可以放在您的 neoan3 模型的 callStatic 中。

$customTransformerClass 默认追踪到模型的转换器,但也可以提供(例如 SomeTransfomer::class)。

$assumesUniqueIdsInDb 默认为 true,并假设 BINARY(16) 数据库处理 neoan3-apps/db(必需)使用。如果设置为 false,则期望自增整数。

$customPathForMigrationJSON 可用于使用除模型中现有的以外的迁移 JSON,但这不推荐。

$transformerInstance = new Transformer($customTransformerClass, $modelName, $assumesUniqueIdsInDb = true, $customPathForMigrationJSON = false)

或者,您可以创建 Transformer 的实例。请注意,在这种情况下,您必须提供转换器(类)和模型(字符串)。

转换器

一个转换器(model/some/Some.transformer.php)可以看起来像这样

<?php

namespace Neoan3\Model;


class SomeTransformer implements IndexTransformer
{
    static function modelStructure()
    {
        return [
            'name' => [
                'required' => true,
                'on_read' => function($input,$all){ return $input . ' (human)';}
            ],
            'assignments' => [
                'translate' => 'task_assignment',
                'depth' => 'many',
                'required_fields' => ['user_id']
            ]           
        ];
    }
}

转换器定义了 CRUD 操作的行为,以下监听器

  • on_read
  • on_update
  • on_creation
  • on_delete

neoan3 模型处理假设每个实体在数据库中都有一个主表和可能关联的从表。是否为一对一或一对多可以通过 "depth"(one | many)来表示。