【问题标题】:Codeigniter 3 with Doctrine 2 - Doctrine\ORM\Mapping\MappingException Class is not a valid entity or mapped super classCodeigniter 3 与 Doctrine 2 - Doctrine\ORM\Mapping\MappingException 类不是有效的实体或映射的超类
【发布时间】:2018-07-13 15:20:36
【问题描述】:

我现在已经存在很多关于同样错误的问题,但所有这些问题都与使用 YML 或 Symfony/Zend 作为框架有关。我正在学习在我的 Codeigniter 项目中使用 Doctrine,并且已经到了使用 cli 工具生成实体的地步:

php cli-doctrine.php orm:convert-mapping --from-database annotation models/entities

瞧,我所有的实体都生成了。即使我在 bootstrap.php 中指定了我想使用实体命名空间,所有生成的类都不使用该命名空间。无论如何,我只是手动添加的。这是我在 application/models/Entity/Vatpercentage.php 中的实体 Vatpercentage 的示例:

    namespace Entity;

    use Doctrine\ORM\Mapping as ORM;


    /**
     * Entity\Vatpercentage
     *
     * @ORM\Table(name="vatpercentage", uniqueConstraints={@ORM\UniqueConstraint(name="id_UNIQUE", columns={"id"}), @ORM\UniqueConstraint(name="code_UNIQUE", columns={"code"}), @ORM\UniqueConstraint(name="vat_UNIQUE", columns={"vat"})})
     * @ORM\Entity
     */
    class Vatpercentage
    {
        /**
         * @var integer
         *
         * @ORM\Column(name="id", type="integer", nullable=false)
         * @ORM\Id
         * @ORM\GeneratedValue(strategy="IDENTITY")
         */
        private $id;

        /**
         * @var integer
         *
         * @ORM\Column(name="vat", type="integer", nullable=false)
         */
        private $vat = '0';

        /**
         * @var string
         *
         * @ORM\Column(name="code", type="string", length=45, nullable=false)
         */
        private $code;

        /**
         * @var integer
         *
         * @ORM\Column(name="accountnr", type="integer", nullable=true)
         */
        private $accountnr;


    }

现在我想调用 EntityManager 并查看是否可以从我的数据库中检索我的实体之一。我的模型中的代码:

public function get_vatpercentages(){
    $result = $this->doctrine->em->find('Entity\Vatpercentage', 1);

然后我得到一个异常:

    An uncaught Exception was encountered
    Type: Doctrine\ORM\Mapping\MappingException

    Message: Class "Entity\Vatpercentage" is not a valid entity or mapped super class.

    Filename: /Users/pimdietz/Documents/programming/pos/vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/MappingException.php

    Line Number: 346

我不知道为什么。

为了完整起见,这也是我在 library/doctrine.php 中的引导程序:

include_once FCPATH . 'vendor/autoload.php';

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

class Doctrine {

    public $em;

    public function __construct() {
        // 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/Entity');

        //Dev mode disables caching methods, otherwise it will try Apc, memcache, etc.:
        $dev_mode = ENVIRONMENT == 'development';

        // If you want to use a different metadata driver, change createAnnotationMetadataConfiguration
        // to createXMLMetadataConfiguration or createYAMLMetadataConfiguration.
        $config = Setup::createAnnotationMetadataConfiguration($metadata_paths, $dev_mode, $proxies_dir);
        $this->em = EntityManager::create($connection_options, $config);
        $loader = new ClassLoader($models_namespace, $models_path);
        $loader->register();
    }

}

拜托,漂亮漂亮,谁能告诉我我做错了什么?

【问题讨论】:

  • 我从未使用过 CI3 和 Doctrine,但 thisthis 可能会有所帮助!
  • 不幸的是,我的实现是基于这两个帖子的,我已经知道它们了...但是感谢您的帮助!
  • 如果您还没有尝试过this,您可以试试吗?很抱歉无法提供帮助,但我没有活跃的 CI3 和 Doctrine 项目,所以我自己尝试谷歌搜索并尝试提供帮助。
  • 解决了,使用了错误的annotationreader...

标签: php doctrine-orm codeigniter-3 mappingexception


【解决方案1】:

解决了!

我发现我使用的是 SimpleAnnotationReader,它不喜欢我使用的 orm cli 工具命令生成的注释:

php cli-doctrine.php orm:convert-mapping --from-database annotation models/entities

正如您在 Doctrine\ORM\Tools\Setup 类的方法“createAnnotationMetadataConfiguration”中看到的,最后一个参数标记了 simpleannotationreader 的使用:

        /**
         * Creates a configuration with an annotation metadata driver.
         *
         * @param array   $paths
         * @param boolean $isDevMode
         * @param string  $proxyDir
         * @param Cache   $cache
         * @param bool    $useSimpleAnnotationReader
         *
         * @return Configuration
         */
        public static function createAnnotationMetadataConfiguration(array $paths, $isDevMode = false, $proxyDir = null, Cache $cache = null, $useSimpleAnnotationReader = true)
        {
            $config = self::createConfiguration($isDevMode, $proxyDir, $cache);
            $config->setMetadataDriverImpl($config->newDefaultAnnotationDriver($paths, $useSimpleAnnotationReader));

            return $config;
        }

简而言之,我需要做的就是给它一个错误标志以使用 simpleannotationreader(在我的 Doctrine.php 引导程序中):

$config = Setup::createAnnotationMetadataConfiguration($metadata_paths, $dev_mode, $proxies_dir, null, false);

现在它可以正常工作了!

【讨论】:

    猜你喜欢
    • 2016-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-20
    • 2019-11-10
    • 2014-01-08
    • 1970-01-01
    相关资源
    最近更新 更多