如何从数据库生成 Doctrine 实体并使用 PSR-4 自动加载?

How to generate Doctrine entities From Database and Use PSR-4 Autoloading?

使用 Doctrine 2.5 和 PSR-4 自动加载并将已设计的数据库模式转换为实体 classes(注释)。问题是在正确的目录结构中获取导出的文件。

composer.json

{
    "autoload": {
        "psr-4": {
            "Application\": "src/"
        }
    },
    "require": {
        "doctrine/orm": "^2.5"
    }
}

orm:转换映射

vendor/bin/doctrine orm:convert-mapping \
    --namespace='Application\Entity\' \
    --force \
    --from-database  \
    annotation \
    src/

运行 此命令将在 src/ 中添加一个 Application 目录。 生成的 class 文件具有正确的命名空间,但在 PSR-4 标准的错误目录中。

<?php

namespace Application\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * User
 *
 * @ORM\Table(name="user")
 * @ORM\Entity
 */
class User
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;


}

有没有办法不用辅助命令来解决这个问题?

我没有从 doctrine cli 中看到任何需要的选项。我刚刚看到从您的应用程序模块修改目录结构的解决方案。这里我修改了composer.json

{
    "autoload": {
        "psr-4": {
            "Application\": "src/Application/"
        }
    },
    "require": {
        "doctrine/orm": "^2.5"
    }
}

所有 Application 模块源代码将放在 src/Application 而不是 src/ 上。因此,当 doctrine clisrc 中创建目录 Application/Entity 时,它将与您的 psr-4 自动加载器匹配。