2

我正在通过reversenginerring在codeigniter中使用学说制作实体(即我有我的数据库并且我通过运行命令制作了实体orm:convert-mapping --form-database annotation models/Entity

我的学说.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();



    }

}

我在运行命令后得到的实体是这样的......

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

这里发生了什么建议?

4

1 回答 1

0

您是否尝试过清除缓存?

我刚刚遇到了同样的问题并清除了缓存就可以了!

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

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

或者删除cache.app/cache

祝你好运 :)

于 2014-04-18T16:41:30.063 回答