hamichen/laminas-doctrine-orm

简单的Laminas/Mezzio Doctrine ORM集成

1.0.1 2022-08-23 08:34 UTC

This package is not auto-updated.

Last update: 2024-09-18 14:07:58 UTC


README

简单的Laminas/Mezzio Doctrine ORM集成。

请随意报告错误和缺失的功能。

使用

配置

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

  • doctrine - Doctrine配置的键
    • 连接
      • orm_default
        • driver_class - \Doctrine\DBAL\Driver实现的全名
        • params - 驱动程序参数
    • driver - 映射驱动程序配置
      • orm_default
        • drivers
          • 键必须是实体(例如 App\Entity)的命名空间
            • class - MappingDriver的全名
            • 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

待办事项

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

原因

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