phpdoctrine-ormcodeigniter-3mappingexception

Codeigniter 3 with Doctrine 2 - Doctrine\ORM\Mapping\MappingException Class is not a valid entity or mapped super class


I now a lot of questions already exist about this same error, but all of them are with using YML or Symfony/Zend as framework. I am learning to use Doctrine with my Codeigniter project, and I have gotten upto the point where I generated the Entities using the cli tool:

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

And voila, all my entities are generated. Even though I specified in my bootstrap.php I want to use the Entity namespace all the generated classes don't use that namespace. Anyway, I just added that manually. Here is an example of my Entity Vatpercentage in 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;


    }

Now I want to call the EntityManager and see if I can retrieve one of my entities from my database. Code in my model:

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

But then I get an Exception:

    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

And I cannot figure out why.

For completeness, here is also my bootstrapper in 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();
    }

}

Please, pretty pretty please, can anyone tell me what I am doing wrong?


Solution

  • Solved it!

    I found out I was using the SimpleAnnotationReader, which doesn't like the annotations generated by the orm cli tool command I used:

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

    As you can see here in the method "createAnnotationMetadataConfiguration" of the Doctrine\ORM\Tools\Setup class the last parameter flags the use of the 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;
            }
    

    So in short, All I needed to do to make it work was to give it the false flag for using the simpleannotationreader (in my Doctrine.php bootstrapper):

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

    And now it works fine!