11-16-2019, 08:26 PM
(This post was last modified: 11-16-2019, 08:31 PM by ScientiFist.)
Hi,
I'm trying to integrate doctrine 2.6.4 with CI 4.
Doctrine was installed via composer.
App/Libraries/Doctrine.php
App/Doctrine.php
Everything is working with the console i can render Entities via YML Mappings so everything is connected to my db and so on
Then i'm just calling doctrine in the Base controller in CI 3 i was using the load->library
And here is the problem i can't initiate the entityManager i tried in different way but im all the time getting a undefined property or undefinedclass :
I know it's maybe complicated or me who's missing something but would be amazing any kind of help, a lot of users of CI are using doctrine as an orm and would really help me for my future project
I'm trying to integrate doctrine 2.6.4 with CI 4.
Doctrine was installed via composer.
App/Libraries/Doctrine.php
Code:
<?php
include_once dirname(__DIR__, 2) . '/vendor/autoload.php';
use Doctrine\Common\ClassLoader;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\Mapping\Driver\YamlDriver;
class Doctrine
{
public $em;
public function __construct()
{
$connection_options = array(
'driver' => 'pdo_mysql',
'user' => 'root',
'password' => 'password',
'host' => 'localhost',
'dbname' => 'project',
'charset' => 'utf8',
'driverOptions' => array(
'charset' => 'utf8',
),
);
$models_namespace = 'Entities';
$models_path = APPPATH . 'Models';
$proxies_dir = APPPATH . 'Models/Proxies';
$metadata_paths = array(APPPATH . 'Models/Entities');
$dev_mode = true;
$cache = null;
$useSimpleAnnotationReader = false;
$config = Setup::createAnnotationMetadataConfiguration($metadata_paths, $dev_mode, $proxies_dir, $cache, $useSimpleAnnotationReader);
$this->em = EntityManager::create($connection_options, $config);
//YAML
$yamlDriver = new YamlDriver(APPPATH . 'Models/Mappings');
$config->setMetadataDriverImpl($yamlDriver);
$loader = new ClassLoader($models_namespace, $models_path);
$loader->register();
}
}
App/Doctrine.php
Code:
<?php
define('APPPATH', dirname(__FILE__) . '/');
define('ENVIRONMENT', 'development');
chdir(APPPATH);
require __DIR__ . '/Libraries/Doctrine.php';
foreach ($GLOBALS as $helperSetCandidate) {
if ($helperSetCandidate instanceof \Symfony\Component\Console\Helper\HelperSet) {
$helperSet = $helperSetCandidate;
break;
}
}
$doctrine = new Doctrine;
$em = $doctrine->em;
$helperSet = new \Symfony\Component\Console\Helper\HelperSet(array(
'db' => new \Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper($em->getConnection()),
'em' => new \Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper($em)
));
\Doctrine\ORM\Tools\Console\ConsoleRunner::run($helperSet);
Everything is working with the console i can render Entities via YML Mappings so everything is connected to my db and so on
Then i'm just calling doctrine in the Base controller in CI 3 i was using the load->library
Quote:require APPPATH.'/Libraries/Doctrine.php';
And here is the problem i can't initiate the entityManager i tried in different way but im all the time getting a undefined property or undefinedclass :
PHP Code:
<?php namespace App\Controllers;
class Home extends BaseController
{
public function index()
{
//TEST 1
$em = $this->doctrine->getEntityManager();
//TEST 2 with the name of my entities
$test= new DebugTest();
return view('welcome_message');
}
}
I know it's maybe complicated or me who's missing something but would be amazing any kind of help, a lot of users of CI are using doctrine as an orm and would really help me for my future project