学说 2 - 没有元数据类需要由 orm:generate-repositories 处理

2022-08-30 21:03:40

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

没有要处理的元数据类

我已经找到了

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

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

我按照下面页面的说明进行操作,但没有运气。我知道我很接近,但缺少文件路径或命名空间的东西。请帮忙。

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

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

cli-config,

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\列等)

<?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()
    {

    }

}

答案 1

编辑3:

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

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

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

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

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

顺便说一句,我注意到在您的用户实体类中,您有.生成的文件不应该创建吗?或者也许这是一个错别字。use Doctrine\Mapping as ORMuse Doctrine\Orm\Mapping as ORM;

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

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

例:

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

答案 2

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

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

以下是创建注释元数据配置方法的文档。我刚刚深入研究了源代码。默认情况下,它使用简单的注释读取器,这意味着您不需要在注释前面有ORM\,您可以执行@Entities而不是@ORM\实体。因此,您需要做的就是使用简单的注释阅读器禁用它。

/**
 * 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)

推荐