如何使用 ClassMetadataBuilder 从 Doctrine PHP 映射生成实体

How to generate entities from Doctrine PHP mapping using ClassMetadataBuilder

我正在尝试使用 ClassMetadataBuilder 配置实体,但我似乎找不到使用

从 class 生成实体的方法
php vendor/bin/doctrine orm:generate-entities src

我的示例代码来自文档,看起来像:

class User {

    private $id;
    private $name;

    public static function loadMetadata(ClassMetadata $metadata) {
        $builder = new ClassMetadataBuilder($metadata);
        $builder->createField('id', 'integer')->isPrimaryKey()->generatedValue()->build();
        $builder->addField('name', 'string');
    }

}

当我使用基于注释的配置时,我可以使用以下 cli-config.php:

生成实体
$entitiesPath = array('src/Entity');
$isDevMode = $configuration->getValue('mode') === 'TEST';
$config = Setup::createAnnotationMetadataConfiguration($entitiesPath, $isDevMode);

$conn = $db->getConnectionDetails();
$entityManager = EntityManager::create($conn, $config);

return ConsoleRunner::createHelperSet($entityManager);

我应该在配置中使用什么元数据驱动程序而不是 Setup::createAnnotationMetadataConfiguration?还是我做的 PHP 映射完全错误?

我最终跳过了命令行并在我的 PHP 代码中使用了 EntityGenerator

//generator code
(new \Doctrine\ORM\Tools\EntityGenerator())->
    generate(array(MyMetadataLoader::loadMetadata()), "src/Entity");

//metadata loader
public static function loadMetadata() {
    $metadata = new \Doctrine\ORM\Mapping\ClassMetadata("Entity/User");
    $builder = new ClassMetadataBuilder($metadata);
    $builder->setTable('user');
    $builder->createField('id', 'integer')->isPrimaryKey()->generatedValue()->build();
    $builder->addField('name', 'string');
    return $builder->getClassMetadata();
}

并用于创建表格

$em = MyEntityManagerLoader::loadEntityManager();
$tool = new \Doctrine\ORM\Tools\SchemaTool($em);
$classes = array(
    MyMetadataLoader::loadMetadata()
);
$tool->createSchema($classes);