【问题标题】:Doctrine 2 - No Metadata Classes to process by orm:generate-repositoriesDoctrine 2 - orm:generate-repositories 没有要处理的元数据类
【发布时间】:2012-04-03 01:45:50
【问题描述】:

我正在尝试生成实体存储库并收到此类消息

没有要处理的元数据类

我已经找到了

使用 Doctrine\ORM\Mapping 作为 ORM; 和 @ORM\表 无法正常工作。

如果我将所有@ORM\Table 更改为仅@Table(和其他注释)- 它开始工作,但我真的不想那样做,因为它应该与@ORM 注释一起工作。

我按照以下页面中的说明进行操作,但没有成功。我知道我很接近但缺少文件路径或名称空间的一些东西。请帮忙。

http://docs.doctrine-project.org/projects/doctrine-common/en/latest/reference/annotations.html

有人遇到过这样的问题吗?我错过了什么?

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(工作版本,最初如描述,@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()
    {

    }

}

【问题讨论】:

  • 我刚才也遇到了同样的问题。如果我能找到解决方案,我会发布答案。
  • 贴出实体和阅读器的代码,以及目录结构可能有助于获得准确的答案。
  • 添加了 cli config 和 User.php 的源代码
  • 哇 @450 代表赏金 :o,希望我知道足够的教义来帮助你:(

标签: php orm doctrine-orm


【解决方案1】:

除了 Symfony2 项目之外,我在任何地方都找不到对 @ORM\Table 的任何引用。在文档中它总是被引用为@Table

我知道它在 sf2 中有效(我在那里使用它)。这可能是 Doctrine 中的香草安装的错误吗?

【讨论】:

  • 不知道为什么,文档中省略了命名空间。
  • 我认为文档不是最新的(令人沮丧)。
【解决方案2】:
..

$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');

..

【讨论】:

    【解决方案3】:

    我刚刚遇到了和你一样的问题。我正在使用 Doctrine 2.4。我可以通过在配置文件中执行此操作来解决此问题。我不确定这是否适用于版本

    $config = Setup::createAnnotationMetadataConfiguration(array(__DIR__."/src/entities"), $isDevMode, null, null, FALSE); // just add null, null, false at the end
    

    以下是方法 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)
    

    【讨论】:

      【解决方案4】:

      [英文]

      查看 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);
      

      注意:路径 /config/yml 必须存在。

      [西班牙语]

      Revisar el archivo bootstrap y donde configuras el orm 学说,cambia las annotaciones por yaml:

      /* 通过注解配置*/ //$config = Setup::createAnnotationMetadataConfiguration(array(DIR."/src"), $isDevMode);

      /* Configuring by yaml*/
      $config = Setup::createYAMLMetadataConfiguration(array(__DIR__."/config/yml"), $isDevMode);
      

      重要提示:el directorio /config/yml debe existsir.

      【讨论】:

        【解决方案5】:

        我的问题出在 bootstrap.php(cli-config.php 需要)

        $config = Setup::createAnnotationMetadataConfiguration(array(__DIR__."/src"), $isDevMode);
        

        这个“src”没有指向正确的源文件夹。

        【讨论】:

          【解决方案6】:

          正如 Gohn67 所说..你必须实例化一个新读者。

          我有同样的问题,但 Zend。 问题出在阅读器上,而不是在驱动程序上。

          例如: 如果我使用“Doctrine\Common\Annotations\SimpleAnnotationReader”作为阅读器,我必须在没有@ORM 的情况下编写所有注释

          但如果我使用“Doctrine\Common\Annotations\AnnotationReader”,我需要将@ORM 放在注释上才能工作

          【讨论】:

            【解决方案7】:

            注意到一个小的差异......

            在您的实体中使用;

            use Doctrine\Mapping as ORM;
            

            代替:

            use Doctrine\ORM\Mapping as ORM;
            

            也许这会解决它?

            【讨论】:

              【解决方案8】:

              编辑 3:

              如果重要的话,我使用的是 Doctrine 2.2.1。无论如何,我只是在这个主题上添加更多信息。

              我研究了 Doctrine\Configuration.php 类,看看 newDefaultAnnotationDriver 是如何创建 AnnotationDriver 的。该方法从第 125 行开始,但如果您使用的是最新版本的通用库,则相关部分是第 145 到 147 行。

              } else {
                  $reader = new AnnotationReader();
                  $reader->setDefaultAnnotationNamespace('Doctrine\ORM\Mapping\\');
              }
              

              我实际上在 AnnotationReader 类中找不到 setDefaultAnnotationNamespace 方法。所以这很奇怪。但我假设它设置了命名空间 Doctrine\Orm\Mapping,因此该命名空间中的注释不需要加上前缀。因此出现错误,因为教义 cli 工具似乎以不同的方式生成实体。我不确定这是为什么。

              您会在下面的回答中注意到,我没有调用 setDefaultAnnotationNamespace 方法。

              附带说明,我在您的用户实体类中注意到您有use Doctrine\Mapping as ORM。生成的文件不应该创建use Doctrine\Orm\Mapping as ORM;吗?或者这可能是一个错字。

              编辑 1: 好的,我发现了问题。显然它与 \Doctrine\ORM\Configuration 类使用的默认注释驱动程序有关。

              因此,您需要实例化一个新的 AnnotationReader、一个新的 AnnotationDriver,而不是使用 $config-&gt;newDefaultAnnotationDriver(...),然后在您的 Configuration 类中进行设置。

              例子:

              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(此处将调整添加到您的 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)
              ));
              

              【讨论】:

              • 你是救生员。我使用了一个旧的 Symfony 项目直接从数据库表生成实体映射,然后将实体类复制到这个新的 Doctrine 项目中。你的回答很到位。我找到了“@ORM\”并在我的实体类上替换了“@”,瞧。现在我实际上在教义控制台中显示了实体元数据。
              【解决方案9】:

              在从 Doctrine 2.0 升级到 Doctrine 2.1(或 2.2)时,我遇到了类似的问题(尽管反过来)。对于 Doctrine 2.0,我使用 @Table 的注释工作正常,但升级后它开始抱怨没有加载注释。我建议你试试 Doctrine 2.2,以便使用 @ORM\Table

              【讨论】:

                【解决方案10】:

                正如您所说,最可能的解释是,阅读器或实体中的包含(命名空间问题、路径问题等)有问题。

                【讨论】:

                  猜你喜欢
                  • 2018-07-08
                  • 1970-01-01
                  • 2015-08-13
                  • 1970-01-01
                  • 2014-04-11
                  • 2019-01-12
                  • 2012-08-18
                  • 1970-01-01
                  • 2018-06-11
                  相关资源
                  最近更新 更多