php Doctrine 2 - 没有要由 orm:generate-repositories 处理的元数据类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9755518/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Doctrine 2 - No Metadata Classes to process by orm:generate-repositories
提问by waney
I'm trying to generate entity repositories and getting such message
我正在尝试生成实体存储库并收到此类消息
No Metadata Classes to process
没有要处理的元数据类
I'd tracked down that use of
我追踪到了
use Doctrine\ORM\Mapping as ORM; and @ORM\Table is not working properly.
使用 Doctrine\ORM\Mapping 作为 ORM;并且@ORM\Table 无法正常工作。
If i change all @ORM\Table to just @Table(and other annotations) - it start to work, but I really don't want to get it that way as it should work with @ORM annotation.
如果我将所有 @ORM\Table 更改为仅 @Table(和其他注释) - 它开始工作,但我真的不想那样做,因为它应该与 @ORM 注释一起使用。
I followed instructions from page below with no luck. I know I'm close but missing something with file paths or namespaces. Please help.
我按照下面页面的说明操作,但没有运气。我知道我很接近但缺少文件路径或命名空间的某些内容。请帮忙。
http://docs.doctrine-project.org/projects/doctrine-common/en/latest/reference/annotations.html
http://docs.doctrine-project.org/projects/doctrine-common/en/latest/reference/annotations.html
Does anyone had such problem? What I missing?
有没有人有这样的问题?我错过了什么?
cli-config,
cli配置,
use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\Common\Annotations\AnnotationRegistry;
require_once 'Doctrine/Common/ClassLoader.php';
define('APPLICATION_ENV', "development");
error_reporting(E_ALL);
//AnnotationRegistry::registerFile("Doctrine/ORM/Mapping/Driver/DoctrineAnnotations.php");
//AnnotationRegistry::registerAutoloadNamespace("Symfony\Component\Validator\Constraint", "Doctrine/Symfony");
//AnnotationRegistry::registerAutoloadNamespace("Annotations", "/Users/ivv/workspaceShipipal/shipipal/codebase/application/persistent/");
$classLoader = new \Doctrine\Common\ClassLoader('Doctrine');
$classLoader->register();
$classLoader = new \Doctrine\Common\ClassLoader('Entities', __DIR__ . '/application/');
$classLoader->register();
$classLoader = new \Doctrine\Common\ClassLoader('Proxies', __DIR__ . '/application/persistent');
$classLoader->register();
$config = new \Doctrine\ORM\Configuration();
$config->setProxyDir(__DIR__ . '/application/persistent/Proxies');
$config->setProxyNamespace('Proxies');
$config->setAutoGenerateProxyClasses((APPLICATION_ENV == "development"));
$driverImpl = $config->newDefaultAnnotationDriver(array(__DIR__ . "/application/persistent/Entities"));
$config->setMetadataDriverImpl($driverImpl);
if (APPLICATION_ENV == "development") {
$cache = new \Doctrine\Common\Cache\ArrayCache();
} else {
$cache = new \Doctrine\Common\Cache\ApcCache();
}
$config->setMetadataCacheImpl($cache);
$config->setQueryCacheImpl($cache);
$connectionOptions = array(
'driver' => 'pdo_mysql',
'host' => '127.0.0.1',
'dbname' => 'mydb',
'user' => 'root',
'password' => ''
);
$em = \Doctrine\ORM\EntityManager::create($connectionOptions, $config);
$platform = $em->getConnection()->getDatabasePlatform();
$platform->registerDoctrineTypeMapping('enum', 'string');
$helperSet = new \Symfony\Component\Console\Helper\HelperSet(array(
'db' => new \Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper($em->getConnection()),
'em' => new \Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper($em)
));
User.php(working version, initially it was as described, @Table was @ORM\Table and other annotations similar had @ORM\ part like @ORM\Column etc)
User.php(工作版本,最初是如描述的那样,@Table 是@ORM\Table 和其他类似的注释有@ORM\ 部分,如@ORM\Column 等)
<?php
namespace Entities;
use Doctrine\Mapping as ORM;
/**
* User
*
* @Table(name="user")
* @Entity(repositoryClass="Repository\User")
*/
class User
{
/**
* @var integer $id
*
* @Column(name="id", type="integer", nullable=false)
* @Id
* @GeneratedValue
*/
private $id;
/**
* @var string $userName
*
* @Column(name="userName", type="string", length=45, nullable=false)
*/
private $userName;
/**
* @var string $email
*
* @Column(name="email", type="string", length=45, nullable=false)
*/
private $email;
/**
* @var text $bio
*
* @Column(name="bio", type="text", nullable=true)
*/
private $bio;
public function __construct()
{
}
}
采纳答案by Gohn67
EDIT 3:
编辑 3:
If it matters, I'm using Doctrine 2.2.1. Anyway, I'm just adding a bit more information on this topic.
如果重要的话,我使用的是Doctrine 2.2.1。无论如何,我只是添加了有关此主题的更多信息。
I dug around the Doctrine\Configuration.php class to see how newDefaultAnnotationDriver created the AnnotationDriver. The method begins on line 125, but the relevant part is line 145 to 147 if you're using the latest version of the Common library.
我仔细研究了 Doctrine\Configuration.php 类,看看 newDefaultAnnotationDriver 如何创建 AnnotationDriver。该方法从第 125 行开始,但如果您使用的是最新版本的 Common 库,则相关部分是第 145 至 147 行。
} else {
$reader = new AnnotationReader();
$reader->setDefaultAnnotationNamespace('Doctrine\ORM\Mapping\');
}
I couldn't actually find the setDefaultAnnotationNamespace method in AnnotationReader class. So that was weird. But I'm assuming it sets the namespace Doctrine\Orm\Mapping, so that annotations in that namespace don't need to be prefixed. Hence the error since it seems the doctrine cli tool generates the entities differently. I'm not sure why that is.
我实际上无法在 AnnotationReader 类中找到 setDefaultAnnotationNamespace 方法。所以这很奇怪。但我假设它设置了命名空间 Doctrine\Orm\Mapping,因此该命名空间中的注释不需要前缀。因此错误,因为学说 cli 工具似乎以不同的方式生成实体。我不确定为什么会这样。
You'll notice in my answer below, I didn't call the setDefaultAnnotationNamespace method.
您会在下面的回答中注意到,我没有调用 setDefaultAnnotationNamespace 方法。
One a side note, I noticed in your User Entity class that you have use Doctrine\Mapping as ORM
. Shouldn't the generated file create use Doctrine\Orm\Mapping as ORM;
? Or maybe that is a typo.
一个旁注,我注意到在你的 User Entity 类中你有use Doctrine\Mapping as ORM
. 生成的文件不应该创建use Doctrine\Orm\Mapping as ORM;
吗?或者也许这是一个错字。
EDIT 1:Ok, I found the problem. Apparently it has to do with the default annotation driver used by the \Doctrine\ORM\Configuration class.
编辑 1:好的,我发现了问题。显然它与 \Doctrine\ORM\Configuration 类使用的默认注释驱动程序有关。
So instead of using $config->newDefaultAnnotationDriver(...)
, you need to instantiate a new AnnotationReader, a new AnnotationDriver, and then set it in your Configuration class.
因此$config->newDefaultAnnotationDriver(...)
,您需要实例化一个新的 AnnotationReader、一个新的 AnnotationDriver,而不是使用,然后在您的 Configuration 类中设置它。
Example:
例子:
AnnotationRegistry::registerFile("Doctrine/ORM/Mapping/Driver/DoctrineAnnotations.php");
$reader = new AnnotationReader();
$driverImpl = new \Doctrine\ORM\Mapping\Driver\AnnotationDriver($reader, array(__DIR__ . "/application/persistent/Entities"));
$config->setMetadataDriverImpl($driverImpl);
EDIT2(Here the adjustments added to your cli-config.php):
EDIT2(这里的调整添加到您的 cli-config.php):
use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\Common\Annotations\AnnotationRegistry;
require_once 'Doctrine/Common/ClassLoader.php';
define('APPLICATION_ENV', "development");
error_reporting(E_ALL);
$classLoader = new \Doctrine\Common\ClassLoader('Doctrine');
$classLoader->register();
$classLoader = new \Doctrine\Common\ClassLoader('Entities', __DIR__ . '/application/');
$classLoader->register();
$classLoader = new \Doctrine\Common\ClassLoader('Proxies', __DIR__ . '/application/persistent');
$classLoader->register();
$config = new \Doctrine\ORM\Configuration();
$config->setProxyDir(__DIR__ . '/application/persistent/Proxies');
$config->setProxyNamespace('Proxies');
$config->setAutoGenerateProxyClasses((APPLICATION_ENV == "development"));
//Here is the part that needs to be adjusted to make allow the ORM namespace in the annotation be recognized
#$driverImpl = $config->newDefaultAnnotationDriver(array(__DIR__ . "/application/persistent/Entities"));
AnnotationRegistry::registerFile("Doctrine/ORM/Mapping/Driver/DoctrineAnnotations.php");
$reader = new AnnotationReader();
$driverImpl = new \Doctrine\ORM\Mapping\Driver\AnnotationDriver($reader, array(__DIR__ . "/application/persistent/Entities"));
$config->setMetadataDriverImpl($driverImpl);
//End of Changes
if (APPLICATION_ENV == "development") {
$cache = new \Doctrine\Common\Cache\ArrayCache();
} else {
$cache = new \Doctrine\Common\Cache\ApcCache();
}
$config->setMetadataCacheImpl($cache);
$config->setQueryCacheImpl($cache);
$connectionOptions = array(
'driver' => 'pdo_mysql',
'host' => '127.0.0.1',
'dbname' => 'mydb',
'user' => 'root',
'password' => ''
);
$em = \Doctrine\ORM\EntityManager::create($connectionOptions, $config);
$platform = $em->getConnection()->getDatabasePlatform();
$platform->registerDoctrineTypeMapping('enum', 'string');
$helperSet = new \Symfony\Component\Console\Helper\HelperSet(array(
'db' => new \Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper($em->getConnection()),
'em' => new \Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper($em)
));
回答by Zorji
I just ran into the same problem that you've got. I am using Doctrine 2.4. I can fix this issue by doing this in the config file. I am not sure if this works for versions < 2.3.
我刚刚遇到了和你一样的问题。我正在使用 Doctrine 2.4。我可以通过在配置文件中执行此操作来解决此问题。我不确定这是否适用于 < 2.3 的版本。
$config = Setup::createAnnotationMetadataConfiguration(array(__DIR__."/src/entities"), $isDevMode, null, null, FALSE); // just add null, null, false at the end
Below is the documentation for the method createAnnotationMetadataConfiguration. I have just dig into the source code. By default it uses a simple annotation reader, which means you don't need to have ORM\ in front of your annotation, you can do @Entities instead of @ORM\Entities. So all you need to do here is to disable it using simple annotation reader.
下面是 createAnnotationMetadataConfiguration 方法的文档。我刚刚深入研究了源代码。默认情况下,它使用一个简单的注释阅读器,这意味着您不需要在注释前面有 ORM\,您可以使用 @Entities 而不是 @ORM\Entities。所以你需要做的就是使用简单的注释阅读器禁用它。
/**
* Creates a configuration with an annotation metadata driver.
*
* @param array $paths
* @param boolean $isDevMode
* @param string $proxyDir
* @param Cache $cache
* @param bool $useSimpleAnnotationReader
*
* @return Configuration
*/
public static function createAnnotationMetadataConfiguration(array $paths, $isDevMode = false, $proxyDir = null, Cache $cache = null, $useSimpleAnnotationReader = true)
回答by Quaid
As Gohn67 said.. u have to instanciate a new reader.
正如 Gohn67 所说......你必须实例化一个新的读者。
I had the same issue but with Zend. The problem its in the reader and not in the driver.
我有同样的问题,但与 Zend。问题在于阅读器而不是驱动程序。
Ex: if i use "Doctrine\Common\Annotations\SimpleAnnotationReader" as reader i had to write all my annotation without the @ORM
例如:如果我使用“Doctrine\Common\Annotations\SimpleAnnotationReader”作为读者,我必须在没有@ORM 的情况下编写所有注释
But if i use "Doctrine\Common\Annotations\AnnotationReader" i need to put @ORM on the annotations to get work
但是如果我使用“Doctrine\Common\Annotations\AnnotationReader”,我需要将@ORM 放在注释上才能工作
回答by alvaro
My problem was in bootstrap.php (required by cli-config.php)
我的问题是在 bootstrap.php (cli-config.php 需要)
$config = Setup::createAnnotationMetadataConfiguration(array(__DIR__."/src"), $isDevMode);
this "src" was not pointing to the correct source folder.
这个“src”没有指向正确的源文件夹。
回答by Luillyfe
[English]
[英语]
Review the bootstrap.phpfile and where you configure the orm doctrine, you change the annotations by yaml:
查看bootstrap.php文件,在配置 orm 原则的地方,通过 yaml 更改注释:
/* Configuring by annotacions*/
//$config = Setup::createAnnotationMetadataConfiguration(array(__DIR__."/src"), $isDevMode);
/* Configuring by yaml*/
$config = Setup::createYAMLMetadataConfiguration(array(__DIR__."/config/yml"), $isDevMode);
Note: the path /config/yml must exist.
注意:路径 /config/yml 必须存在。
[Espanish]
[西班牙语]
Revisar el archivo bootstrap y donde configuras el orm doctrine, cambia las anotaciones por yaml:
Revisar el archivo bootstrap y donde configuras el orm 学说,cambia las anotaciones por yaml:
/* Configuring by annotacions*/ //$config = Setup::createAnnotationMetadataConfiguration(array(DIR."/src"), $isDevMode);
/* 通过注解配置*/ //$config = Setup::createAnnotationMetadataConfiguration(array( DIR."/src"), $isDevMode);
/* Configuring by yaml*/
$config = Setup::createYAMLMetadataConfiguration(array(__DIR__."/config/yml"), $isDevMode);
Importante: el directorio /config/yml debe existir.
重要提示:el directoryio /config/yml debeexistir。
回答by Max
..
$generator = new EntityGenerator();
$generator->setAnnotationPrefix(''); // edit: quick fix for No Metadata Classes to process
$generator->setUpdateEntityIfExists(true); // only update if class already exists
//$generator->setRegenerateEntityIfExists(true); // this will overwrite the existing classes
$generator->setGenerateStubMethods(true);
$generator->setAnnotationPrefix('ORM\'); // <<---------------|
$generator->setGenerateAnnotations(true);
$generator->generate($metadata, __DIR__ . '/Entities');
..
回答by Jon Skarpeteig
I hit a similar issue (although the other way around), when upgrading from Doctrine 2.0 to Doctrine 2.1 (or 2.2). For Doctrine 2.0 my annotations using @Table worked fine, but after upgrade it started to complain that annotation was not loaded. I suggest you give Doctrine 2.2 a go, in order to use @ORM\Table
从 Doctrine 2.0 升级到 Doctrine 2.1(或 2.2)时,我遇到了类似的问题(尽管相反)。对于 Doctrine 2.0,我使用 @Table 的注释工作正常,但升级后它开始抱怨未加载注释。我建议你试试 Doctrine 2.2,以便使用 @ORM\Table
回答by Lee Davis
Noticed a small discrepancy...
注意到一个小的差异...
In your Entity your using;
在您的实体中使用;
use Doctrine\Mapping as ORM;
Instead of:
代替:
use Doctrine\ORM\Mapping as ORM;
Maybe that will fix it?
也许这会解决它?
回答by SkaveRat
I can't find any references to @ORM\Table
anywhere but in Symfony2 projects. In the documentation it's always referenced as @Table
@ORM\Table
除了在 Symfony2 项目中,我在任何地方都找不到任何引用。在文档中它总是被引用为@Table
I know that it works in sf2 (I'm using it there). Is it possible that it's a bug with a vanilla install from Doctrine?
我知道它在 sf2 中有效(我在那里使用它)。是否有可能是 Doctrine 的 vanilla 安装的错误?
回答by Hakan Deryal
The most possible explanation is, as you stated, there is something wrong with include(namespace issue, path issue etc.) either in reader or in the entity.
正如您所说,最可能的解释是,在阅读器或实体中包含(命名空间问题、路径问题等)存在问题。