1
votes

I'm using Codeigniter 3 and Doctrine 2, which is loaded by a library class. Doctrine itself is loaded via composer autoload.

On localhost (windows with php 5.6.0) I'm using the php built in server and everything is working. When I'm uploading the project to my webserver (Plesk with nginx and proxied apache, php 5.6.15), i get the following error:

An uncaught Exception was encountered

Type: Doctrine\Common\Persistence\Mapping\MappingException

Message: Class 'Entity\User' does not exist

Filename: /var/www/vhosts/example.org/project.example.org/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/MappingException.php    
Line Number: 96

Backtrace:

File: /var/www/vhosts/example.org/project.example.org/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/RuntimeReflectionService.php
Line: 41
Function: nonExistingClass

File: /var/www/vhosts/example.org/project.example.org/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/AbstractClassMetadataFactory.php
Line: 282
Function: getParentClasses

File: /var/www/vhosts/example.org/project.example.org/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/AbstractClassMetadataFactory.php
Line: 313
Function: getParentClasses

File: /var/www/vhosts/example.org/project.example.org/vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php
Line: 78
Function: loadMetadata

File: /var/www/vhosts/example.org/project.example.org/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/AbstractClassMetadataFactory.php
Line: 216
Function: loadMetadata

File: /var/www/vhosts/example.org/project.example.org/vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php
Line: 281
Function: getMetadataFor

File: /var/www/vhosts/example.org/project.example.org/vendor/doctrine/orm/lib/Doctrine/ORM/Repository/DefaultRepositoryFactory.php
Line: 44
Function: getClassMetadata

File: /var/www/vhosts/example.org/project.example.org/vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php
Line: 698
Function: getRepository

File: /var/www/vhosts/example.org/project.example.org/application/controllers/User.php
Line: 18
Function: getRepository

File: /var/www/vhosts/example.org/project.example.org/index.php
Line: 301
Function: require_once

The controller is as follows:

....
public function show($id)
{
    $user = $this->doctrine->em->getRepository('Entity\User')->findOneBy(array('id' => $id));
}
....

and the doctrine library: use Doctrine\Common\ClassLoader, Doctrine\ORM\Tools\Setup, Doctrine\ORM\EntityManager;

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');
        // Set $dev_mode to TRUE to disable caching while you develop
        $dev_mode = true;
        // 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();
    }

}

The library is loaded via the autoload.php:

$autoload['libraries'] = array('OAuth2', 'session', 'doctrine');

the entity is located in models\Entity\

<?php
namespace Entity;
use Doctrine\ORM\Mapping\Entity;

defined('BASEPATH') OR exit('No direct script access allowed');


/**
 * User Model
 *
 * @Entity
 * @Table(name="user")
 */
class User {

    /**
     * @Id
     * @Column(type="integer", nullable=false)
     * @GeneratedValue(strategy="AUTO")
     */
     protected $id;
....

The strange thing is that it works locally. Any ideas?

1

1 Answers

0
votes

Found the problem: Case-sensitivity of linux:

The folder of the entities should be capitalized according to the namespace. Since the directory was models\entity\ and Windows' file system is case-insensitiv, it worked on locally. But linux is case-sensitiv and expects models\Entity. Renamed the folder to models\Entity and it's working.