带有消息'Class Entity \ Ssclass的未捕获异常'Doctrine \ ORM \ Mapping \ MappingException'不是有效实体或映射超类

时间:2013-07-28 08:16:29

标签: php doctrine-orm codeigniter-2

我通过reversenginerring在codeigniter中使用doctrine创建实体(即我有我的数据库,我通过运行命令orm:convert-mapping --form-database annotation models/Entity来创建实体)

我的doctrine.php是这样的......

<?php

use Doctrine\Common\ClassLoader,
    Doctrine\ORM\Tools\Setup,
    Doctrine\ORM\EntityManager;


class Doctrine
{

    public $em;

    public function __construct()
    {
        require_once __DIR__ . '/Doctrine/ORM/Tools/Setup.php';
        Setup::registerAutoloadDirectory(__DIR__);

        // Load the database configuration from CodeIgniter
        require APPPATH . 'config/database.php';

        $connection_options = array(
            'driver'        => 'pdo_mysql',
            'user'          => $db['default']['username'],
            'password'      => $db['default']['password'],
            'host'          => $db['default']['hostname'],
            'dbname'        => $db['default']['database'],
            'charset'       => $db['default']['char_set'],
            'driverOptions' => array(
                'charset'   => $db['default']['char_set'],
            ),
        );

        // With this configuration, your model files need to be in application/models/Entity
        // e.g. Creating a new Entity\User loads the class from application/models/Entity/User.php
        $models_namespace = 'Entity';
        $models_path = APPPATH . 'models';
        $proxies_dir = APPPATH . 'models/Proxies';
        $metadata_paths = array(APPPATH . 'models');

        // Set $dev_mode to TRUE to disable caching while you develop
        $config = Setup::createAnnotationMetadataConfiguration($metadata_paths, $dev_mode = true, $proxies_dir);
        $this->em = EntityManager::create($connection_options, $config);
        //changing enum to string coz enum is not support as default by doctrine :)
        $platform = $this->em->getConnection()->getDatabasePlatform();
$platform->registerDoctrineTypeMapping('enum', 'string');
        $loader = new ClassLoader($models_namespace, $models_path);
        $loader->register();



    }

}

运行commnad后得到的实体就是这样......

<?php

namespace Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * SsClass
 *
 * @ORM\Table(name="ss_class")
 * @ORM\Entity
 */
class SsClass
{
    /**
     * @var integer $classId
     *
     * @ORM\Column(name="class_id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $classId;

    /**
     * @var string $class
     *
     * @ORM\Column(name="class", type="string", length=200, nullable=true)
     */
    private $class;

    /**
     * @var string $classContent
     *
     * @ORM\Column(name="class_content", type="string", length=500, nullable=true)
     */
    private $classContent;

    /**
     * @var string $isApproved
     *
     * @ORM\Column(name="is_approved", type="string", nullable=true)
     */
    private $isApproved;

    /**
     * @var string $isActive
     *
     * @ORM\Column(name="is_active", type="string", nullable=true)
     */
    private $isActive;
    public function getClassId() {
        return $this->classId;
    }

    public function setClassId($classId) {
        $this->classId = $classId;
    }

    public function getClass() {
                return $this->class;
    }

    public function setClass($class) {
        $this->class = $class;
    }

    public function getClassContent() {
        return $this->classContent;
    }

    public function setClassContent($classContent) {
        $this->classContent = $classContent;
    }

    public function getIsApproved() {
        return $this->isApproved;
    }

    public function setIsApproved($isApproved) {
        $this->isApproved = $isApproved;
    }

    public function getIsActive() {
        return $this->isActive;
    }

    public function setIsActive($isActive) {
        $this->isActive = $isActive;
    }





}

我正在尝试通过我的控制器名称从数据库获取数据作为欢迎控制器..

public function index()

    {
        //$user=new Entity\SsClass;
      $data=$this->em->find('Entity\Ssclass',1);
      print_r($data);

       //echo $user->get(); 
       die();
                        }

}

但是,当我运行此代码时,我将错误视为

Fatal error: Uncaught exception 'Doctrine\ORM\Mapping\MappingException' with message 'Class Entity\Ssclass is not a valid entity or mapped super class.' in D:\xampp\htdocs\new_doctrine\application\libraries\Doctrine\ORM\Mapping\MappingException.php:147 Stack trace: #0 D:\xampp\htdocs\new_doctrine\application\libraries\Doctrine\ORM\Mapping\Driver\AnnotationDriver.php(165): Doctrine\ORM\Mapping\MappingException::classIsNotAValidEntityOrMappedSuperClass('Entity\Ssclass') #1 D:\xampp\htdocs\new_doctrine\application\libraries\Doctrine\ORM\Mapping\ClassMetadataFactory.php(293): Doctrine\ORM\Mapping\Driver\AnnotationDriver->loadMetadataForClass('Entity\Ssclass', Object(Doctrine\ORM\Mapping\ClassMetadata)) #2 D:\xampp\htdocs\new_doctrine\application\libraries\Doctrine\ORM\Mapping\ClassMetadataFactory.php(178): Doctrine\ORM\Mapping\ClassMetadataFactory->loadMetadata('Entity\Ssclass') #3 D:\xampp\htdocs\new_doctrine\application\libraries\Doctrine\ORM\EntityManager.php(269): Doctrine\ORM\Mapping\ClassMetadataFactory->getMetadataFor( in D:\xampp\htdocs\new_doctrine\application\libraries\Doctrine\ORM\Mapping\MappingException.php on line 147

这里有什么建议?

1 个答案:

答案 0 :(得分:0)

您是否尝试过清除缓存?

我遇到了同样的问题并清除缓存了!

您可以像这样使用symfony2命令行:

$ php app/console cache:clear [--env=yourenv]

或删除cache中的app/cache文件夹。

祝你好运:)