XXX不是有效实体或映射的超类//和配置选项

时间:2012-12-20 10:08:50

标签: php class configuration doctrine-orm entity

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

我的电脑上没有安装eAccelerator。

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

  • MyApp的
    • 应用
      • 实体
      • 外部
        • 学说

我用以下内容初始化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

1 个答案:

答案 0 :(得分: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);