<?php
namespace Foo;
use Doctrine\ORM\Mapping as ORM;
/**
* Test
*
*@Table(name="test")
*@Entity
*/
class Test
{
/**
* @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=8, nullable=false)
*/
private $name;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $name
* @return Test
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
}
<?php
// http://wildlyinaccurate.com/integrating-doctrine-2-with-codeigniter-2
use Doctrine\Common\ClassLoader,
Doctrine\ORM\Tools\Setup,
Doctrine\ORM\EntityManager;
class Doctrine
{
public $em;
public function __construct()
{
require_once __DIR__ . '/Doctrine/ORM/Tools/Setup.php';
Setup::registerAutoloadDirectory(__DIR__);
// 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 = 'Foo';
$models_path = APPPATH . 'models';
$proxies_dir = APPPATH . 'models/Proxies';
$metadata_paths = array(APPPATH . 'models');
//Setup::registerAutoloadDirectory(__DIR__);
// Set $dev_mode to TRUE to disable caching while you develop
$config = Setup::createAnnotationMetadataConfiguration($metadata_paths, $dev_mode = true, $proxies_dir);
$this->em = EntityManager::create($connection_options, $config);
$loader = new ClassLoader($models_namespace, $models_path);
$loader->register();
}
}
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class My_Controller extends CI_Controller {
// Doctrine EntityManager
public $em;
function __construct() {
parent::__construct();
$this->load->library('doctrine');
$this->em = $this->doctrine->em;
}
function index() {
$user = new Foo\Test;
}
}
In the debugger I can see that Doctrine.php registers my ‘Foo’ classloader with the correct namespace and includepath. When it comes to execute $user = new Foo\Test; the invoked classloader object’s namespace is ‘Doctrine’ and the includepath is application/libraries/Doctrine therefore loadClass() fails. This seems to be the class loader registered via Setup::registerAutoloadDirectory(DIR); line 15 of Doctrine.php
Any idea what is happening here? Would be very grateful for any suggestions as to how to fix this!