2

我刚开始使用 Doctrine2,当我想持久化一个实体时出现错误。这是我的错误: “类“Myapp\app\entity\user”不是有效的实体或映射的超类。'

我的电脑上没有安装 eAccelerator。

我的应用程序(我不使用 Symfony)结构是这样的:

  • 我的应用
    • 应用程序
      • 实体
      • 外部的
        • 教义

我用以下命令初始化 Doctrine:

$classLoader = new ClassLoader('Doctrine', CORE_PATH . 'external');
$classLoader->register();

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

$doctrineConfig = new \Doctrine\ORM\Configuration();

$doctrineConfig->setMetadataCacheImpl($cache);
$doctrineConfig->setQueryCacheImpl($cache);

$driverImpl = $doctrineConfig->newDefaultAnnotationDriver(array(APP_PATH . 'entity'));
$doctrineConfig->setMetadataDriverImpl($driverImpl);

$doctrineConfig->setAutoGenerateProxyClasses(false);

$connectionOptions = array(
    'driver'   => $database['driver'],
    'dbname'   => $database['dbname'],
    'user'     => $database['username'],
    'password' => $database['password']
);

$this->em = EntityManager::create($connectionOptions, $doctrineConfig);

这些线有什么问题?

另外,当我使用自动加载器自动加载我的类时,是否有必要使用 Doctrine 类加载器?如果是,这些行是否正确?

$classLoader = new ClassLoader('myApp\app\entity', APP_PATH . 'entity');
$classLoader->register();

$classLoader = new ClassLoader('myApp\core\entity', CORE_PATH . 'entity');
$classLoader->register();

编辑:我忘了添加一个实体类

使用 Doctrine\ORM\Mapping 作为 ORM;

/**
 * @ORM\Entity
 */
class user
{
/**
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;

/**
* @ORM\Column(name="username", type="string", length=255)
*/
protected $username;

/**
* @ORM\Column(name="nicename", type="string", length=255)
*/
protected $nicename;

/**
* @ORM\Column(name="email", type="string", length=255)
*/
protected $email;

/**
* @ORM\Column(name="password", type="string", length=255)
*/
protected $password;

//getters
//setters
4

2 回答 2

1

php > 8.0

您必须config/packages/doctrine.yaml将映射类型更改为attribute


doctrine:
    dbal:
        url: '%env(resolve:DATABASE_URL)%'

        # IMPORTANT: You MUST configure your server version,
        # either here or in the DATABASE_URL env var (see .env file)
        #server_version: '13'
    orm:
        auto_generate_proxy_classes: true
        naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
        auto_mapping: true
        mappings:
            App:
                is_bundle: false
                type: attribute # from annotation to attribute
                dir: '%kernel.project_dir%/src/Entity'
                prefix: 'App\Entity'
                alias: App
```
于 2022-01-13T15:07:24.143 回答
0

好的,所以我的实体是正确的,我不需要调用 classLoader。

下面是正确的初始化:

    $classLoader = new ClassLoader('Doctrine', CORE_PATH . 'external/');
    $classLoader->register();

    AnnotationRegistry::registerFile(CORE_PATH.'external/Doctrine/ORM/Mapping/Driver/DoctrineAnnotations.php');

    $cache = new ArrayCache();

    $annotationReader = new AnnotationReader();

    $cacheReader = new CachedReader(
        $annotationReader, 
        $cache          
    );

    $driverChain = new DriverChain();

    $annotationDriver = new AnnotationDriver(
        $annotationReader,
        array(CORE_PATH . 'model/')
    );

    $driverChain->addDriver($annotationDriver, 'gallib\core\model');

    $doctrineConfig = new Configuration();

    $doctrineConfig->setProxyDir(CORE_PATH . 'model/proxy');
    $doctrineConfig->setProxyNamespace('gallib\core\model\proxy');
    $doctrineConfig->setAutoGenerateProxyClasses(true);
    $doctrineConfig->setMetadataDriverImpl($driverChain);
    $doctrineConfig->setMetadataCacheImpl($cache);
    $doctrineConfig->setQueryCacheImpl($cache);

    $database = $this->config->database;

    $connectionOptions = array(
        'driver'   => $database['driver'],
        'dbname'   => $database['dbname'],
        'user'     => $database['username'],
        'password' => $database['password']
    );

    $this->em = EntityManager::create($connectionOptions, $doctrineConfig);
于 2012-12-20T23:01:24.410 回答