简体   繁体   中英

Uncaught exception 'Doctrine\ORM\Mapping\MappingException' with message 'Class Entity\Ssclass is not a valid entity or mapped super class

I am making entities using doctrine in codeigniter by reversenginerring (ie i had my database and i had made the entities by running the command orm:convert-mapping --form-database annotation models/Entity )

my doctrine.php goes like this ...

<?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();



    }

}

and the entities which i get after running the commnad goes like this ...

<?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;
    }





}

I am trying to fetch the data from database by my controller name as welcome controller as ..

public function index()

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

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

}

But when i run this code i gote the error as

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

What is going on here any suggestion??

Have you tried clearing your cache ?

I just ran into the same problem and clearing the cache did it !

You can use the symfony2 command line like so:

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

Or delete the cache folder in app/cache .

Good luck :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM