sirix/cycle-orm-factory

Mezzio 的 Cycle ORM 工厂

1.0.11 2024-04-21 20:05 UTC

This package is auto-updated.

Last update: 2024-09-21 20:59:18 UTC


README

GitHub license

本软件包为将 Cycle ORM 集成到 Mezzio 框架中提供工厂,提供无缝的设置和配置。

安装

composer require sirix/cycle-orm-factory

配置

创建一个配置文件,例如,config/autoload/cycle-orm.global.php

<?php

declare(strict_types=1);

use Cycle\Database\Config;
use Sirix\Cycle\Enum\SchemaProperty;


return [
    'cycle'           => [
        'default'     => 'default',
        'databases'   => [
            'default' => [
                'connection' => 'mysql',
            ]
        ],
        'connections' => [
            'mysql' => new Config\MySQLDriverConfig(
                connection: new Config\MySQL\TcpConnectionConfig(
                    database: 'cycle-orm',
                    host: '127.0.0.1',
                    port: 3306,
                    user: 'cycle',
                    password: 'password'
                ),
                reconnect: true,
                timezone: 'UTC',
                queryCache: true,
            ),
        ]
    ],
    'migrator'        => [
        'directory' => 'db/migrations',
        'table'     => 'migrations'
    ],
    'entities'        => [
        'src/App/src/Entity',
    ],
    'schema'   => [
        'property' => SchemaProperty::GenerateMigrations,
        'cache'    => true,
        'directory' => 'data/cache'
    ],
];

迁移器配置

'migrator' => [
    'directory' => 'db/migrations',
    'table'     => 'migrations',
],
  • directory:指定存储迁移文件的目录。
  • table:指定用于存储迁移信息的表名。

实体配置

'entities' => [
    'src/App/src/Entity',
],
  • 指定实体类所在的目录。

模式配置

'schema' => [
    'property' => null,
    'cache'    => true,
    'directory' => 'data/cache'
],
  • property:配置模式属性,选项包括 nullSchemaProperty::SyncTablesSchemaProperty::GenerateMigrations
  • cache:启用或禁用生成的模式的缓存。
  • directory:指定存储缓存的模式的目录。

模式属性选项

SchemaProperty::SyncTables

SyncTables 选项根据提供的实体类同步数据库表。它确保表与实体类中定义的结构相匹配。这在数据库模式与您的应用程序一起演变时开发期间非常有用。

SchemaProperty::GenerateMigrations

GenerateMigrations 选项用于根据在实体类中检测到的更改自动生成迁移文件。启用后,ORM 分析当前数据库模式与定义的实体之间的差异。然后生成迁移文件以应用这些更改,使版本控制和管理工作更轻松。

根据您项目的需求选择合适的 SchemaProperty 选项。根据您的需求自定义配置,并享受 Cycle ORM 与 Mezzio 的无缝集成。

在项目中使用

$container->get('orm'); // Cycle\ORM\ORM
$container->get('dbal'); // Cycle\Database\DatabaseInterface
$container->get('migrator'); // Cycle\Migrations\Migrator

这些工厂提供了在 Mezzio 框架中无缝使用 Cycle ORM 所需的组件。根据您项目的需求自定义配置。

有关 Cycle ORM 的更多信息,请参阅 Cycle ORM 文档

迁移器命令

Sirix\Cycle\Command\Migrator 命名空间提供了两个命令来管理使用 Cycle ORM 的数据库迁移。这些命令旨在与 laminas-cli 工具一起使用。

1. migrator:migrate 命令

描述

migrator:migrate 命令执行必要的数据库迁移步骤,将迁移文件中指定的更改应用于同步数据库模式。

用法

php vendor/bin/laminas migrator:migrate

选项

此命令没有其他选项。

2. migrator:rollback 命令

描述

migrator:rollback 命令撤销最后迁移所做的更改,将数据库模式恢复到其以前的状态。

用法

php vendor/bin/laminas migrator:rollback

选项

此命令没有其他选项。

注意:请确保已正确配置项目配置文件中的数据库连接和迁移设置。

有关使用 Cycle ORM 进行迁移的更多信息,请参阅 Cycle ORM 文档