具有教义2的Codeigniter 3-Doctrine \ ORM \ Mapping \ MappingException类不是有效实体或映射的超类

时间:2018-07-13 15:20:37

标签: php doctrine-orm codeigniter-3 mappingexception

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

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

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

    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

我不知道为什么。

出于完整性考虑,这也是我在libraries / 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();
    }

}

请,很漂亮,任何人都可以告诉我我在做什么错吗?

1 个答案:

答案 0 :(得分:0)

解决了!

我发现我正在使用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);

现在工作正常!