[eluser]Unknown[/eluser]
Hello, After a long day i've gotten CI2 to work with Doctrine 2 with module extensions.
I followed this
tutorial to get me started.
After a discussion in the #doctrine channel i was able to get it to work in any location.
the Doctrine.php file i altered to look like this.
Code:
<?php
use Doctrine\Common\ClassLoader,
Doctrine\ORM\Configuration,
Doctrine\ORM\EntityManager,
Doctrine\Common\Cache\ArrayCache,
Doctrine\DBAL\Logging\EchoSqlLogger;
class Doctrine {
public $em = null;
public function __construct()
{
// load database configuration from CodeIgniter
//require_once APPPATH.'config/database.php';
include APPPATH.'config/database.php';
// Set up class loading. You could use different autoloaders, provided by your favorite framework,
// if you want to.
require_once APPPATH.'libraries/Doctrine/Common/ClassLoader.php';
$doctrineClassLoader = new ClassLoader('Doctrine', APPPATH.'libraries');
$doctrineClassLoader->register();
//$entitiesClassLoader = new ClassLoader('models', rtrim(APPPATH, '/'));
//$entitiesClassLoader->register();
$proxiesClassLoader = new ClassLoader('Proxies', APPPATH.'models/proxies');
$proxiesClassLoader->register();
// Set up caches
$config = new Configuration;
$cache = new ArrayCache;
$config->setMetadataCacheImpl($cache);
$config->setQueryCacheImpl($cache);
// Set up driver
$Doctrine_AnnotationReader = new \Doctrine\Common\Annotations\AnnotationReader($cache);
$Doctrine_AnnotationReader->setDefaultAnnotationNamespace('Doctrine\ORM\Mapping\\');
$driver = new \Doctrine\ORM\Mapping\Driver\AnnotationDriver($Doctrine_AnnotationReader, APPPATH.'modules');
$config->setMetadataDriverImpl($driver);
// Proxy configuration
$config->setProxyDir(APPPATH.'/models/proxies');
$config->setProxyNamespace('Proxies');
// Set up logger
//$logger = new EchoSqlLogger;
//$config->setSqlLogger($logger);
$config->setAutoGenerateProxyClasses( TRUE );
// Database connection information
$connectionOptions = array(
'driver' => 'pdo_pgsql',
'user' => $db['default']['username'],
'password' => $db['default']['password'],
'host' => $db['default']['hostname'],
'dbname' => $db['default']['database']
);
// Create EntityManager
$this->em = EntityManager::create($connectionOptions, $config);
}
}
Notice how I commented out the classLoader for models and I altered the driver annotations to look in the modules folder.
Thing to know is that it will look in all the sub dir for class annotations, that is why I told it to look in modules.
I included the path to the database.php because require_once was not working properly. Don't know why so when I worked with include I stopped looking for answers.
And that Is pretty much it.
With this setup i dont have to worry about name spaces either.
here is my example controller
Code:
<?php
class Patient extends MX_Controller {
private $em;
function __construct(){
parent::__construct();
$this->em = $this->doctrine->em;
$this->load->model('patient_m','patient');
}
function index(){
$patient = $this->patient;
// var_dump(class_exists('Patient_m', true));
$patient->setFirst_name('lover');
$patient->setLast_name('hater');
$patient->setAge(65);
$this->em->persist($patient);
$this->em->flush();
echo "First Names";
$f = $this->em->getRepository('Patient_m')->findBy(array('age' => 20));
foreach($f as $r){
echo $r->getFirst_name().'<br/>';
}
}
}
?>
and Model
Code:
<?php
/**
*@Entity
*@Table(name="patient")
*/
class Patient_m extends CI_Model {
/** @Id
*@Column(type="integer")
*@GeneratedValue(strategy="SEQUENCE")
*/
private $id;
/** @Column(length=255)*/
private $first_name;
/** @Column(length=255)*/
private $last_name;
/** @Column(type="integer")*/
private $age;
function __construct(){
parent::__construct();
}
public function getFirst_name(){
return $this->first_name;
}
public function getLast_name(){
return $this->last_name;
}
public function getAge(){
return $this->age;
}
public function setFirst_name($n){
$this->first_name = $n;
}
public function setLast_name($n){
$this->last_name = $n;
}
public function setAge($n){
$this->age = $n;
}
}
?>
And there you have it. Works like a charm. As long as your model class exists ($this->load->model('patient_m','patient')
it should work. That was the key to getting this up and running.
If there is anything I missed let me know.
If you have anything to add, Well let people know.
If what I'm doing is wrong and stupid, let me know.