Welcome Guest, Not a member yet? Register   Sign In
Integration doctrine to codeigniter 4
#1

I have installed doctrine with composer and added Doctrine.php to libraries using tutorial on next link
codeigniter and doctrine

and I try this:


PHP Code:
<?php namespace App\Controllers;

use 
CodeIgniter\Controller;
use 
App\Libraries\Doctrine;

use 
Doctrine\ORM\Query\ResultSetMapping;
use 
Doctrine\ORM\EntityManager;
use 
Doctrine\ORM\Tools\Setup;

class 
Home extends Controller {

public function 
test {

            
$rsm = new ResultSetMapping();
 
           
            
            $query 
$entityManager->createNativeQuery('SELECT * FROM test'$rsm);
 
           
            
            $users 
$query->getResult();
 
           print_r($users);

       }



Autoload


PHP Code:
public $classmap = [
 
           'Test' => APPPATH 'libraries/Test.php',
 
           'Twig' => APPPATH 'libraries/Twig.php',
 
           'Doctrine' => APPPATH 'libraries/Doctrine.php',
 
       ]; 


and I get error: Undefined variable: entityManager

This is only the test for me.
Reply
#2

You said where to find EntityManager, but didn't instantiate anything. $entityManager is null.
Are you perhaps missing $entityManager = new EntityManager(); ?
Reply
#3

(06-09-2018, 12:53 PM)ciadmin Wrote: You said where to find EntityManager, but didn't instantiate anything. $entityManager is null.
Are you perhaps missing $entityManager = new EntityManager(); ?

codeigniter and doctrine
Reply
#4

Hmmm - right in the link you sent, it says ...
Code:
// Create EntityManager
$this->em = EntityManager::create($connectionOptions, $config);

I don't see anything like that in your code, and stand by my original answer.
You are also not setting up the rest of the stuff in the constructor, per the article link.
Reply
#5

This is not for CodeIgniter 4 forums ..

$entityManager what is that variable, where you use it and how do you get it ? The error you get is simple PHP error that you are using undefined variable .. (there is nothing set to $entityManager)

At the tutorial it is simple written that you must use :
Code:
$em = $this->doctrine->em;

Is that what you use for $entityManager ?
Best VPS Hosting : Digital Ocean
Reply
#6

Simple hes calling the Entity Manager in his query but it has not yet been initiated
like @ciadmin has said...
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#7

Hi guys, I'm trying to setting up Doctrine with CI4, but I cannot find a valid tutorial on how to do that. I'd like to setup doctrine cli also. Now I have a clean CI4 installation and I add doctrine/orm via composer. How can I go on? Can you help me?

Thanx, bye!
Reply
#8

(This post was last modified: 04-11-2020, 06:00 AM by ufhy.)

Maybe like this:

app\Libraries\Doctrine.php

PHP Code:
<?php namespace App\Libraries;

use 
Doctrine\ORM\EntityManager;
use 
Doctrine\ORM\Tools\Setup;

class 
Doctrine
{
    public $em null;

    public function __construct()
    {
        $dbConfig config('Config\Database');
        $db $dbConfig->{$dbConfig->defaultGroup};

        $isDevMode ENVIRONMENT !== 'production';
        $proxyDir null;
        $cache null;
        $useSimpleAnnotationReader false;

        $config Setup::createAnnotationMetadataConfiguration(
            [APPPATH 'Entities'],
            $isDevMode,
            $proxyDir,
            $cache,
            $useSimpleAnnotationReader
        
);

        $connection = [
            'driver'   => $db['DBDriver'] === 'MySQLi' 'mysqli' 'pdo_mysql',
            'user'     => $db['username'],
            'password' => $db['password'],
            'host'     => $db['hostname'],
            'dbname'   => $db['database'],
            'charset'  => $db['charset'],
        ];

        $this->em EntityManager::create($connection$config);
    }


cli-config.php (folder root project)

PHP Code:
<?php
use App\Libraries\Doctrine;
use 
Doctrine\ORM\Tools\Console\ConsoleRunner;

error_reporting(E_ALL);

$pathsPath './app/Config/Paths.php';

require 
$pathsPath;
$paths = new Config\Paths();

// Location of the framework bootstrap file.
$app = require rtrim($paths->systemDirectory'/ ') . '/bootstrap.php';
$doctrine = new Doctrine();

return 
ConsoleRunner::createHelperSet($doctrine->em); 

and now you can exec command 
Code:
vendor/bin/doctrine
Reply
#9

Hi,

This is my integration vendor for doctrine.

https://github.com/daycry/doctrine
Reply
#10
Thumbs Up 
(This post was last modified: 10-29-2021, 01:06 AM by b126. Edit Reason: added Oracle section )

(05-11-2021, 05:43 AM)daycry Wrote: Hi,

This is my integration vendor for doctrine.

https://github.com/daycry/doctrine
Hi Daycry, 

I just would like to thank you here since it works PERFECTLY !

I remember that the integration of Doctrine with CI3 was a nightmare for me some years ago...
but your integration with CI4 is super smooth and to the point. Easy install, easy config, Excellent job! ?✨

I really recommend it. Thanks again.

For info, I run it out of the box against a Microsoft SQL Server with the native "SQLSRV" driver provided by Code Igniter. 

As the Oracle OCI driver is not yet available at Code Igniter, I am using the PDO_OCI one, but then you need to add some hook in Doctrine.php

        $connectionOptions = [
            'driver'  => 'pdo_oci',
            'user'    => 'myschema',
            'password' => 'mypwd',
            'host'    => 'myserver',
            'dbname'  => 'myschema',
            'charset'  => 'UTF8',
            'servicename'  => 'myssid',
            'port'    => 1521
        ];
Reply




Theme © iAndrew 2016 - Forum software by © MyBB