qumuinc/ulidtypes

CakePHP 的 UlidType 插件

维护者

详细信息

github.com/Muuq/ulidtype

源代码

安装: 569

依赖: 0

建议: 0

安全: 0

星标: 0

关注者: 0

分支: 1

类型:cakephp-plugin

v0.0.5 2021-10-05 01:43 UTC

This package is not auto-updated.

Last update: 2024-10-01 19:50:29 UTC


README

设置

安装

$ composer require qumuinc/ulidtypes

引导

  • config/bootstrap.php 中添加插件加载命令
...
Plugin::load('qumuinc/UlidTypes', ['bootstrap' => true]);
...

模型

  • _initializeSchema() 方法添加到 Model\Table 类中,使 id 类型为 ulid
...
use Cake\Database\Schema\TableSchemaInterface;
...
class XXXXXXXXTable extends Table
{
    protected function _initializeSchema(TableSchemaInterface $table): TableSchemaInterface
    {
        parent::_initializeSchema($table);
        $table->setColumnType('id', 'ulid'); // set ulid type for id

        return $table;
    }
}
...

或者,您可以使用一个特质。

...
use PrefixUlidType\PrefixUlidTypeTrait;
...
class XXXXXXXXTable extends Table
{
    use PrefixUlidTypeTrait;
}
...

如果您想在模型中使用 _initializeSchema 函数,需要调用 _traitInitSchema 函数。

...
use Cake\Database\Schema\TableSchemaInterface;
use PrefixUlidType\PrefixUlidTypeTrait;
...
class XXXXXXXXTable extends Table
{
    use PrefixUlidTypeTrait;

    protected function _initializeSchema(TableSchemaInterface $table): TableSchemaInterface
    {
        parent::_traitInitSchema($table);
        $table->setColumnType('code', 'char'); // set any type for property

        return $table;
    }
}
...