不是有效的实体或映射类错误

发布于 2024-12-03 16:33:56 字数 6123 浏览 0 评论 0原文

我正在将 Doctrine 与 Codeigniter 一起使用。所以我编写了一个库类来一起使用它们。但我无法访问实体(在通过逆向工程从数据库创建实体之后)。 Doctrine 给出错误:致命错误:未捕获异常“Doctrine\ORM\Mapping\MappingException”,消息为“类操作不是有效实体或映射的超类。”

我只是将此情况的代码添加到库类并且一切正常,但速度非常低

    $this->em->getConfiguration()
             ->setMetadataDriverImpl(
                new DatabaseDriver(
                        $this->em->getConnection()->getSchemaManager()
                )
    );

对于此错误我该怎么办?我使用此函数从数据库生成实体:

    $cmf = new DisconnectedClassMetadataFactory();
    $cmf->setEntityManager($this->em);
    $metadata = $cmf->getAllMetadata();
    $generator = new EntityGenerator();

    $generator->setUpdateEntityIfExists(true);
    $generator->setGenerateStubMethods(true);
    $generator->setGenerateAnnotations(true);
    $generator->generate($metadata, APPPATH."models/entities");

我的操作实体:(

<?php



/**
 * Actions
 *
 * @Table(name="actions")
 * @Entity
 */
class Actions
{
    /**
     * @var integer $id
     *
     * @Column(name="id", type="integer", nullable=false)
     * @Id
     * @GeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /**
     * @var string $name
     *
     * @Column(name="name", type="string", length=45, nullable=false)
     */
    public $name;

    /**
     * @var string $nameSafe
     *
     * @Column(name="name_safe", type="string", length=45, nullable=false)
     */
    public $nameSafe;


    /**
     * Get id
     *
     * @return integer $id
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set name
     *
     * @param string $name
     */
    public function setName($name)
    {
        $this->name = $name;
    }

    /**
     * Get name
     *
     * @return string $name
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * Set nameSafe
     *
     * @param string $nameSafe
     */
    public function setNameSafe($nameSafe)
    {
        $this->nameSafe = $nameSafe;
    }

    /**
     * Get nameSafe
     *
     * @return string $nameSafe
     */
    public function getNameSafe()
    {
        return $this->nameSafe;
    }
} 

编辑)我的库代码:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
use Doctrine\Common\ClassLoader,
    Doctrine\ORM\Configuration,
    Doctrine\ORM\EntityManager,
    Doctrine\Common\Cache\ArrayCache,
    Doctrine\DBAL\Logging\EchoSQLLogger,
    Doctrine\ORM\Mapping\Driver\DatabaseDriver,
    Doctrine\ORM\Tools\DisconnectedClassMetadataFactory,
    Doctrine\ORM\Tools\EntityGenerator;

    /**
     * CodeIgniter Doctrine Class
     *
     * initializes basic doctrine settings and act as doctrine object
     *
     * @author  Mehmet Aydın Bahadır
     * @link    http://www.biberltd.com/
     */
    class Doctrine {

          /**
           * @var EntityManager $em
           */
            public $em = null;

          /**
           * constructor
           */
          public function __construct()
          {
            // load database configuration from CodeIgniter
            require APPPATH.'config/database.php';

            // Set up class loading. You could use different autoloaders, provided by your favorite framework,
            // if you want to.
            require_once APPPATH.'third_party/Doctrine/Common/ClassLoader.php';

            $doctrineClassLoader = new ClassLoader('Doctrine',  APPPATH.'third_party');
            $doctrineClassLoader->register();
            $entitiesClassLoader = new ClassLoader('models', rtrim(APPPATH, "/" ));
            $entitiesClassLoader->register();
            $proxiesClassLoader = new ClassLoader('proxies', APPPATH.'models');
            $proxiesClassLoader->register();

            // Set up caches
            $config = new Configuration;
            $cache = new ArrayCache;
            $config->setMetadataCacheImpl($cache);
            $driverImpl = $config->newDefaultAnnotationDriver(array(APPPATH.'models/entities'));
            $config->setMetadataDriverImpl($driverImpl);
            $config->setQueryCacheImpl($cache);

            // Proxy configuration
            $config->setProxyDir(APPPATH.'models/proxies');
            $config->setProxyNamespace('Proxies');

            // Set up logger
            //$logger = new EchoSQLLogger;
            //$config->setSQLLogger($logger);

            $config->setAutoGenerateProxyClasses( TRUE );
            // Database connection information
            $connectionOptions = array(
                'driver' => 'pdo_mysql',
                'user' =>     $db['default']['username'],
                'password' => $db['default']['password'],
                'host' =>     $db['default']['hostname'],
                'dbname' =>   $db['default']['database']
            );

            // Create EntityManager
            $this->em = EntityManager::create($connectionOptions, $config);
//          $this->generate_classes();

          }

          /**
           * generate entity objects automatically from mysql db tables
           * @return none
           */
          public function generate_classes(){     

//            $this->em->getConfiguration()
//                     ->setMetadataDriverImpl(
//                        new DatabaseDriver(
//                                $this->em->getConnection()->getSchemaManager()
//                        )
//            );

            $cmf = new DisconnectedClassMetadataFactory();
            $cmf->setEntityManager($this->em);
            $metadata = $cmf->getAllMetadata();
            $generator = new EntityGenerator();

            $generator->setUpdateEntityIfExists(true);
            $generator->setGenerateStubMethods(true);
            $generator->setGenerateAnnotations(true);
            $generator->generate($metadata, APPPATH."models/entities");

          }

    }
?>

I'm using Doctrine with Codeigniter. So i write a library class for using them together. But i cant access entities (after creating them from db with reverse-engineering). Doctrine gives error: Fatal error: Uncaught exception 'Doctrine\ORM\Mapping\MappingException' with message 'Class Actions is not a valid entity or mapped super class.'

I just add this code for this situation to library class and works everything right but speed is very low:

    $this->em->getConfiguration()
             ->setMetadataDriverImpl(
                new DatabaseDriver(
                        $this->em->getConnection()->getSchemaManager()
                )
    );

What can i do for this error? I generated entities from DB with this function:

    $cmf = new DisconnectedClassMetadataFactory();
    $cmf->setEntityManager($this->em);
    $metadata = $cmf->getAllMetadata();
    $generator = new EntityGenerator();

    $generator->setUpdateEntityIfExists(true);
    $generator->setGenerateStubMethods(true);
    $generator->setGenerateAnnotations(true);
    $generator->generate($metadata, APPPATH."models/entities");

My Action entity:

<?php



/**
 * Actions
 *
 * @Table(name="actions")
 * @Entity
 */
class Actions
{
    /**
     * @var integer $id
     *
     * @Column(name="id", type="integer", nullable=false)
     * @Id
     * @GeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /**
     * @var string $name
     *
     * @Column(name="name", type="string", length=45, nullable=false)
     */
    public $name;

    /**
     * @var string $nameSafe
     *
     * @Column(name="name_safe", type="string", length=45, nullable=false)
     */
    public $nameSafe;


    /**
     * Get id
     *
     * @return integer $id
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set name
     *
     * @param string $name
     */
    public function setName($name)
    {
        $this->name = $name;
    }

    /**
     * Get name
     *
     * @return string $name
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * Set nameSafe
     *
     * @param string $nameSafe
     */
    public function setNameSafe($nameSafe)
    {
        $this->nameSafe = $nameSafe;
    }

    /**
     * Get nameSafe
     *
     * @return string $nameSafe
     */
    public function getNameSafe()
    {
        return $this->nameSafe;
    }
} 

(edit) My Library Code:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
use Doctrine\Common\ClassLoader,
    Doctrine\ORM\Configuration,
    Doctrine\ORM\EntityManager,
    Doctrine\Common\Cache\ArrayCache,
    Doctrine\DBAL\Logging\EchoSQLLogger,
    Doctrine\ORM\Mapping\Driver\DatabaseDriver,
    Doctrine\ORM\Tools\DisconnectedClassMetadataFactory,
    Doctrine\ORM\Tools\EntityGenerator;

    /**
     * CodeIgniter Doctrine Class
     *
     * initializes basic doctrine settings and act as doctrine object
     *
     * @author  Mehmet Aydın Bahadır
     * @link    http://www.biberltd.com/
     */
    class Doctrine {

          /**
           * @var EntityManager $em
           */
            public $em = null;

          /**
           * constructor
           */
          public function __construct()
          {
            // load database configuration from CodeIgniter
            require APPPATH.'config/database.php';

            // Set up class loading. You could use different autoloaders, provided by your favorite framework,
            // if you want to.
            require_once APPPATH.'third_party/Doctrine/Common/ClassLoader.php';

            $doctrineClassLoader = new ClassLoader('Doctrine',  APPPATH.'third_party');
            $doctrineClassLoader->register();
            $entitiesClassLoader = new ClassLoader('models', rtrim(APPPATH, "/" ));
            $entitiesClassLoader->register();
            $proxiesClassLoader = new ClassLoader('proxies', APPPATH.'models');
            $proxiesClassLoader->register();

            // Set up caches
            $config = new Configuration;
            $cache = new ArrayCache;
            $config->setMetadataCacheImpl($cache);
            $driverImpl = $config->newDefaultAnnotationDriver(array(APPPATH.'models/entities'));
            $config->setMetadataDriverImpl($driverImpl);
            $config->setQueryCacheImpl($cache);

            // Proxy configuration
            $config->setProxyDir(APPPATH.'models/proxies');
            $config->setProxyNamespace('Proxies');

            // Set up logger
            //$logger = new EchoSQLLogger;
            //$config->setSQLLogger($logger);

            $config->setAutoGenerateProxyClasses( TRUE );
            // Database connection information
            $connectionOptions = array(
                'driver' => 'pdo_mysql',
                'user' =>     $db['default']['username'],
                'password' => $db['default']['password'],
                'host' =>     $db['default']['hostname'],
                'dbname' =>   $db['default']['database']
            );

            // Create EntityManager
            $this->em = EntityManager::create($connectionOptions, $config);
//          $this->generate_classes();

          }

          /**
           * generate entity objects automatically from mysql db tables
           * @return none
           */
          public function generate_classes(){     

//            $this->em->getConfiguration()
//                     ->setMetadataDriverImpl(
//                        new DatabaseDriver(
//                                $this->em->getConnection()->getSchemaManager()
//                        )
//            );

            $cmf = new DisconnectedClassMetadataFactory();
            $cmf->setEntityManager($this->em);
            $metadata = $cmf->getAllMetadata();
            $generator = new EntityGenerator();

            $generator->setUpdateEntityIfExists(true);
            $generator->setGenerateStubMethods(true);
            $generator->setGenerateAnnotations(true);
            $generator->generate($metadata, APPPATH."models/entities");

          }

    }
?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

顾挽 2024-12-10 16:33:56

解决方案是编辑 php.ini 文件以关闭 eAccelerator:

eaccelerator.enable="0"

Solution is editing php.ini file for closing eAccelerator:

eaccelerator.enable="0"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文