简体   繁体   English

原则2:数据库中生成的实体没有名称空间

[英]Doctrine 2: Generated entities from database don't have namespaces

I am creating entities from the database trough the \\Doctrine\\ORM\\Tools\\DisconnectedClassMetadataFactory() class. 我正在通过\\Doctrine\\ORM\\Tools\\DisconnectedClassMetadataFactory()类从数据库创建实体。 This works perfectly! 这很完美! Except for the namespace generation. 命名空间生成除外。 There are no namespaces generated. 没有生成名称空间。 I am storing my entities in App/Model/Entities . 我将我的实体存储在App/Model/Entities

Does anyone know how to make the generator add namespaces to the entities? 有谁知道如何让生成器向实体添加命名空间?

This is the code I use to generate the entities: 这是我用来生成实体的代码:

<?php
$em->getConfiguration()->setMetadataDriverImpl(
    new \Doctrine\ORM\Mapping\Driver\DatabaseDriver(
        $em->getConnection()->getSchemaManager()
    )
);

$cmf = new \Doctrine\ORM\Tools\DisconnectedClassMetadataFactory();
$cmf->setEntityManager($em);
$metadata = $cmf->getAllMetadata();

// GENERATE PHP ENTITIES!
$entityGenerator = new \Doctrine\ORM\Tools\EntityGenerator(); 
$entityGenerator->setGenerateAnnotations(true); 
$entityGenerator->setGenerateStubMethods(true); 
$entityGenerator->setRegenerateEntityIfExists(false); 
$entityGenerator->setUpdateEntityIfExists(true); 
$entityGenerator->generate($metadata, __dir__. '/Model/Entities");

I think, the better way is set namespace directly to driver 我认为,更好的方法是将命名空间直接设置为驱动程序

<?php
$driver = new \Doctrine\ORM\Mapping\Driver\DatabaseDriver($em->getConnection()->getSchemaManager());
$driver->setNamespace('App\\Model\\Entities\\');

$em->getConfiguration()->setMetadataDriverImpl($driver);

....

I don't think you can set the namespace when importing from the database, because the EntityGenerator takes the namespace information from the metadata. 我不认为您可以在从数据库导入时设置命名空间,因为EntityGenerator从元数据中获取命名空间信息。 The database does not have or need namespaces, so the information does not automatically come from there. 数据库没有或不需要名称空间,因此信息不会自动来自那里。

You could try looping over the $metadata object and adding the namespace yourself to class names. 您可以尝试循环遍历$ metadata对象并自己将名称空间添加到类名。 The code in the EntityGenerator that retrieves the namespace is quite simple: EntityGenerator中检索命名空间的代码非常简单:

private function _getNamespace(ClassMetadataInfo $metadata)
{
    return substr($metadata->name, 0, strrpos($metadata->name, '\\'));
}

If all else fails, you can always implement your own EntityGenerator that you can feed a namespace to use. 如果所有其他方法都失败了,您始终可以实现自己的EntityGenerator,您可以提供要使用的命名空间。 We did that in our large project, where there are some other custom tasks to be done during generation. 我们在我们的大型项目中做到了这一点,在这个项目中,在生成过程中还有其他一些自定义任 However, the EntityGenerator is not easily overriden, lots of private methods and properties so you probably have to copy&paste the whole thing. 但是,EntityGenerator不容易被覆盖,有很多私有方法和属性,所以你可能需要复制和粘贴整个东西。

You may use this PHP script to generate entities from database tables. 您可以使用此PHP脚本从数据库表生成实体。 This script adds a sample namespace to generated files, so you can set your Namespace instead. 此脚本将一个示例命名空间添加到生成的文件中,因此您可以设置命名空间。 https://gist.github.com/SamvelG/3b39622844f23cac7e76#file-doctrine-entity-generator-php https://gist.github.com/SamvelG/3b39622844f23cac7e76#file-doctrine-entity-generator-php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM