在数据库中插入一个新条目时,第2.2条中的未捕获异常


Uncaught exception on Doctrine 2.2 while insert a new entry in database

我对条令2.2有一个奇怪的问题。我尝试在数据库中插入一个新条目,但遇到了以下问题。这意味着什么?我该如何解决

Fatal error: Uncaught exception 'Doctrine'ORM'Mapping'MappingException' with message 'Class Model'User is not a valid entity or mapped super class.' in C:'xampp'htdocs'www'DoctrineExplained'DoctrineORM'Doctrine'ORM'Mapping'MappingException.php:147
Stack trace: 
#0 C:'xampp'htdocs'www'DoctrineExplained'DoctrineORM'Doctrine'ORM'Mapping'Driver'AnnotationDriver.php(166): Doctrine'ORM'Mapping'MappingException::classIsNotAValidEntityOrMappedSuperClass('Model'User')
#1 C:'xampp'htdocs'www'DoctrineExplained'DoctrineORM'Doctrine'ORM'Mapping'ClassMetadataFactory.php(293): Doctrine'ORM'Mapping'Driver'AnnotationDriver->loadMetadataForClass('Model'User', Object(Doctrine'ORM'Mapping'ClassMetadata))
#2 C:'xampp'htdocs'www'DoctrineExplained'DoctrineORM'Doctrine'ORM'Mapping'ClassMetadataFactory.php(178): Doctrine'ORM'Mapping'ClassMetadataFactory->loadMetadata('Model'User')
#3 C:'xampp'htdocs'www'DoctrineExplained'DoctrineORM'Doctrine'ORM'EntityManager.php(271): Doctrine'ORM'Mapping'ClassMetadataFactory->getMe in C:'xampp'htdocs'www'DoctrineExplained'DoctrineORM'Doctrine'ORM'Mapping'MappingException.php on line 147

我的项目目录结构如下:

/测试

DoctrineORM

DoctrineORM/bin

条令RM/bin/条令

型号/

模型/用户.php

test.php

最后,这些是我在test.phpModel/User.php中的代码

--- Model/User.php ---
<?php
//    Model File model/User.php 
namespace Model;
/** @Entity @Table(name="users") */
class User
{
    /**
     * @Id @Column(type="integer")
     * @GeneratedValue(strategy="AUTO")
     */
    public $id;

   /** @Column(type="string", length=50) */
    public $name;

  /** @Column(type="integer", length=50) */
    public $age;
}
?>

最后的代码在test.php

<?php
////    test.php

require 'DoctrineORM/Doctrine/Common/ClassLoader.php';
$classLoader = new 'Doctrine'Common'ClassLoader('Doctrine', 'DoctrineORM');
$classLoader->register(); 
$classloader = new 'Doctrine'Common'ClassLoader('model', __DIR__);
$classloader->register();
$config = new 'Doctrine'ORM'Configuration();
$cache = new 'Doctrine'Common'Cache'ArrayCache();
$config->setMetadataCacheImpl($cache);
$config->setQueryCacheImpl($cache);
$driverImpl = $config->newDefaultAnnotationDriver('model');
$config->setMetadataDriverImpl($driverImpl);
$config->setProxyDir('proxies');
$config->setProxyNamespace('proxies');
$config->setAutoGenerateProxyClasses(true);
$config->getAutoGenerateProxyClasses();
$connectionOptions = array(
    'driver' => 'pdo_mysql',
    'dbname' => 'test',
    'user' => 'root',
    'password' => '0000'
);
$em = 'Doctrine'ORM'EntityManager::create($connectionOptions, $config);

//    insert a new User to DB
$user = new model'User();
$user->name = 'lorem impum user';
$user->age = 55;
$em->persist($user);
$em->flush(); 

即使在Windows上,区分大小写也很重要。在代码中使用"Model"代替"Model"。

添加"使用模型/用户;"到测试代码的顶部,然后使用"$user=new user()";这将验证您的类加载程序是否正常工作。

并且一定要花点时间(正如Jani所建议的)来弄清楚如何从命令行运行这些东西。你的发展会更快。