Doctrine 2 - 没有元数据类由orm:generate-repositories处理

wan*_*ney 19 php orm doctrine-orm

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

没有要处理的元数据类

我跟踪了那个用法

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

如果我将所有@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)
));
Run Code Online (Sandbox Code Playgroud)

User.php(工作版本,最初是如上所述,@ Table是@ORM\Table和其他注释类似@ORM\part就像@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()
    {

    }

}
Run Code Online (Sandbox Code Playgroud)

Goh*_*n67 19

编辑3:

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

我在Doctrine\Configuration.php类中挖掘了一下newDefaultAnnotationDriver如何创建AnnotationDriver.该方法从第125行开始,但如果您使用的是最新版本的Common库,则相关部分是第145到147行.

} else {
    $reader = new AnnotationReader();
    $reader->setDefaultAnnotationNamespace('Doctrine\ORM\Mapping\\');
}
Run Code Online (Sandbox Code Playgroud)

我实际上找不到AnnotationReader类中的setDefaultAnnotationNamespace方法.所以这很奇怪.但我假设它设置了名称空间Doctrine\Orm\Mapping,因此该命名空间中的注释不需要加前缀.因此,错误似乎是学说cli工具以不同方式生成实体.我不知道为什么会这样.

您将在下面的答案中注意到,我没有调用setDefaultAnnotationNamespace方法.

一个旁注,我在你的用户实体课中注意到你有use Doctrine\Mapping as ORM.生成的文件不应该创建use Doctrine\Orm\Mapping as ORM;吗?或者这可能是一个错字.

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

因此$config->newDefaultAnnotationDriver(...),您需要实例化一个新的AnnotationReader,一个新的AnnotationDriver,然后在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);
Run Code Online (Sandbox Code Playgroud)

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)
));
Run Code Online (Sandbox Code Playgroud)


Zor*_*rji 10

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

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

以下是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)
Run Code Online (Sandbox Code Playgroud)