skar/laminas-doctrine-orm

简单的 Laminas/Mezzio Doctrine ORM 集成

0.2 2024-02-04 05:32 UTC

This package is auto-updated.

Last update: 2024-09-11 19:46:12 UTC


README

简单的 Laminas/Mezzio Doctrine ORM 集成。

请随时报告错误和缺少的功能。

使用

配置

创建配置文件 config/autoload/doctrine.global.php,包含最小配置

  • doctrine - doctrine 配置的键
    • connection
      • orm_default
        • driver_class - \Doctrine\DBAL\Driver 实现的完整名称
        • params - 连接驱动 参数
    • driver - 映射驱动配置
      • orm_default
        • drivers
          • 键必须是实体(例如 App\Entity)的命名空间
            • class - 元数据驱动的完整名称
            • paths - 包含您的 实体 的目录数组

有关更多可能的配置,请参阅 Doctrine 文档

示例配置

<?php
declare(strict_types=1);

use Doctrine\DBAL\Driver\PDO\MySQL\Driver;
use Doctrine\ORM\Mapping\Driver\AnnotationDriver;

return [
    'doctrine' => [
        'connection' => [
            'orm_default' => [
                'driver_class' => Driver::class,
                'params' => [
                    'host'     => 'localhost',
                    'port'     => '3306',
                    'user'     => 'root',
                    'password' => 'root',
                    'dbname'   => 'database',
                ],
            ],
        ],
        
        'driver' => [
            'orm_default' => [
                'drivers' => [
                    'App\Entity' => [
                        'class' => AnnotationDriver::class,
                        'paths' => [
                            'src/Entity',
                        ],
                    ],
                ],
            ],
        ],
    ],
];

迁移命令

命令使用 Laminas CLI 和 vendor/bin/laminas 入口

可能的命令

doctrine:migrations:diff         [diff] Generate a migration by comparing your current database to your mapping information.
doctrine:migrations:dump-schema  [dump-schema] Dump the schema for your database to a migration.
doctrine:migrations:execute      [execute] Execute a single migration version up or down manually.
doctrine:migrations:generate     [generate] Generate a blank migration class.
doctrine:migrations:latest       [latest] Outputs the latest version number
doctrine:migrations:migrate      [migrate] Execute a migration to a specified version or the latest available version.
doctrine:migrations:rollup       [rollup] Rollup migrations by deleting all tracked versions and insert the one version that exists.
doctrine:migrations:status       [status] View the status of a set of migrations.
doctrine:migrations:up-to-date   [up-to-date] Tells you if your schema is up-to-date.
doctrine:migrations:version      [version] Manually add and delete migration versions from the version table.

示例

$ vendor/bin/laminas doctrine:migrations:diff
$ vendor/bin/laminas doctrine:migrations:migrate

TODO

  1. 测试
  2. 将此库拆分为独立的 DBAL、ORM 和迁移库
  3. 添加库支持(认证、分页等)

WHY

因为 DoctrineORMModule 只适用于 Laminas(完整的 MVC),但我需要 Mezzio 的简单集成。