根据当前配置,没有映射的Doctrine ORM实体

Luk*_*uke 5 php mysql doctrine-orm

我有一个可疑的问题.我有一组现有的带注释的Doctrine实体,它们已成功用于Symfony2/Doctrine2项目.但是,我目前正在将这个项目的一些核心功能隔离到它自己的Web框架独立库中,我似乎无法让实体正常运行.

目前我主要担心的是Doctrine CLI实用程序给我的结果好坏参半.

当我执行以下操作时:

bin/doctrine orm:validate-schema
Run Code Online (Sandbox Code Playgroud)

我得到以下输出:

[Mapping]  OK - The mapping files are correct.
[Database] OK - The database schema is in sync with the mapping files.
Run Code Online (Sandbox Code Playgroud)

但当我这样做时:

bin/doctrine orm:info
Run Code Online (Sandbox Code Playgroud)

我明白了:

[Exception]                                                                                                                                                                             
You do not have any mapped Doctrine ORM entities according to the current configuration. If you have entities or mapping files you should check your mapping configuration for errors.
Run Code Online (Sandbox Code Playgroud)

我现在已经完成了数十亿次的配置.我甚至删除了所有实体,并在那里留下了一个最基本的用户实体,给了我相同的场景.

什么可能是这些混合结果的来源?

Luk*_*uke 14

事实证明,标准的Doctrine配置设置[1]不适用于我的代码库,或者我测试过的任何代码库,也许文档已经过时了.在通过Interwebs几个小时后,这是最终使它适用于我的配置:

use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\EntityManager;
use Doctrine\Common\Annotations\AnnotationReader;

$paths = array( realpath(__DIR__."/../src/My/Entity") );
$isDevMode = TRUE;

// the connection configuration
$dbParams = array(
    'driver'   => 'pdo_mysql',
    'user'     => 'myuser',
    'password' => 's3cr3t',
    'dbname'   => 'mydb',
);

$cache = new \Doctrine\Common\Cache\ArrayCache();

$reader = new AnnotationReader();
$driver = new \Doctrine\ORM\Mapping\Driver\AnnotationDriver($reader, $paths);

$config = Setup::createAnnotationMetadataConfiguration($paths, $isDevMode);
$config->setMetadataCacheImpl( $cache );
$config->setQueryCacheImpl( $cache );
$config->setMetadataDriverImpl( $driver );

$entityManager = EntityManager::create($dbParams, $config);

//-- This I had to add to support the Mysql enum type.
$platform = $entityManager->getConnection()->getDatabasePlatform();
$platform->registerDoctrineTypeMapping('enum', 'string');
Run Code Online (Sandbox Code Playgroud)

[1] http://docs.doctrine-project.org/en/latest/tutorials/getting-started.html


Nux*_*win 12

接受的答案是可以的,但同样的事情可以通过不那么冗长的方式实现:

use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\EntityManager;

$paths = array( realpath(__DIR__."/../src/My/Entity") );
$isDevMode = TRUE;

// the connection configuration
$dbParams = array(
    'driver'   => 'pdo_mysql',
    'user'     => 'myuser',
    'password' => 's3cr3t',
    'dbname'   => 'mydb',
);

$config = Setup::createAnnotationMetadataConfiguration(
    $paths, $isDevMode, null, null, false
);
$config->setQueryCacheImpl(new \Doctrine\Common\Cache\ArrayCache());
$entityManager = EntityManager::create($dbParams, $config);

//-- This I had to add to support the Mysql enum type.
$platform = $entityManager->getConnection()->getDatabasePlatform();
$platform->registerDoctrineTypeMapping('enum', 'string');
Run Code Online (Sandbox Code Playgroud)

所有这里都是关于简单注释驱动程序的使用(Setup :: createAnnotationMetadataConfiguration函数的最后一个参数.默认情况下,使用一个简单的注释驱动程序,它不能识别@ORM\Entity注释.

  • 感谢您澄清该解决方案为何有效! (2认同)