我对Doctrine 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中
DoctrineORM / bin中/学说
模型/
模型/ user.php的
test.php的
最后,这些是 test.php 和 Model / 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();
答案 0 :(得分:0)
即使在Windows上,区分大小写也很重要。使用&#39;模型&#39;取代&#39; model&#39;在你的代码中。
添加&#39;使用型号/用户;&#39;在测试代码的顶部,然后使用&#39; $ user = new User()&#39 ;;这将验证您的类加载器是否正常工作。
并且花时间(正如Jani建议的那样)弄清楚如何从命令行运行这些东西。你的发展会更快。