• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Doctrine integration

#13
Hello! Thanks for your tutorial, it helped me a lot!

For my part, I used Entities present in CI modules (folder App\Modules) for the mapping. (I can provide some code snippets if you are interested)

Everything works without problems, however, I was wondering if it was possible to access Doctrine commands on a remote host (OVH for example) for shared servers (in SSH maybe)? Or if we could simply call the commands of Doctrine (update DB schema, ...) without terminal access?

Thanks again!


EDIT: I ended up figuring out how to do it  Big Grin  if someone is interested:

PHP Code:
<?php
namespace DoctrineMod\Controllers;

use 
Doctrine\ORM\Tools\SchemaValidator;
use 
Doctrine\ORM\Tools\ToolsException;

class 
C_Doctrine extends \CodeIgniter\Controller
{
    
/**
     * Updates DB schema
     */
    
public function updateSchema(){
        
$em service('doctrine');
        
$tool = new \Doctrine\ORM\Tools\SchemaTool($em);
        
$tool->updateSchema($this->getEntitiesMetadata());
    }

    
/**
     * Creates DB schema
     */
    
public function createSchema(){
        
$em service('doctrine');
        
$tool = new \Doctrine\ORM\Tools\SchemaTool($em);
        try {
            
$tool->createSchema($this->getEntitiesMetadata());
        } catch (
ToolsException $e) {
            echo 
$e->getMessage();
        }
    }

    
/**
     * Validates current schema and returns eventual errors
     */
    
public function validateSchema(){
        
$em service('doctrine');

        
$validator = new SchemaValidator($em);
        try {
            
$errors $validator->validateMapping();
            if (
count($errors) > 0) {
                echo 
implode("\n\n"$errors);
            }
        } catch (\
Exception $e) {
            echo 
$e->getMessage();
        }
    }

    
/**
     * @return array Entities metadata
     */
    
public function getEntitiesMetadata(){
        
$em service('doctrine');

        
$entities = array();

        
$modulePath APPPATH."Modules";
        foreach (
scandir($modulePath) as $module){
            if(
is_dir($modulePath."/".$module."/Entities")){
                foreach (
scandir($modulePath."/".$module."/Entities") as $moduleName){
                    if (
$moduleName != "." && $moduleName != ".."$entities[] = $em->getClassMetadata(str_replace(".php""",$module."\Entities\\".$moduleName));
                }
            }
        }
        return 
$entities;
    }


Just call these functions to run Doctrine commands directly from a PHP page (without using a terminal).

As a reminder, my Entities are not in the CI4 basic Models\Entities folder. I use modules that have the following structure: app\Modules\{module_name}\Entities.

Here is my Libraries\Doctrine.php class:

PHP Code:
<?php
namespace App\Libraries;

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

include_once 
dirname(__DIR__2) . '/vendor/autoload.php';

class 
Doctrine {

    public 
$em null;

    public function 
__construct()
    {
        
// Retrieving all paths leading to entities classes
        
$modulePath APPPATH."Modules\*\Entities";
        
$entitiesPath glob($modulePathGLOB_ONLYDIR);

        
/*
         * If `$isDevMode` is true caching is done in memory with the ArrayCache. Proxy objects are recreated on every request.
         * If `$isDevMode` is false, check for Caches in the order APC, Xcache, Memcache (127.0.0.1:11211), Redis (127.0.0.1:6379) unless `$cache` is passed as fourth argument.
         * If `$isDevMode` is false, set then proxy classes have to be explicitly created through the command line.
         * If third argument `$proxyDir` is not set, use the systems temporary directory.
         */
        
$isDevMode false;

        if (
function_exists('db_connect')){
            
$db db_connect();
        }

        
// Generating DB connection configuration array
        
$dbParams = array(
            
'driver'   => "mysqli",
            
'user'     => {REDACTED},
            
'password' => {REDACTED},
            
'dbname'   => {REDACTED},
        );

        
$proxies_dir APPPATH 'Models/Proxies';

        
$config Setup::createAnnotationMetadataConfiguration($entitiesPath$isDevMode$proxies_dir);

        try {
            
$this->em EntityManager::create($dbParams$config);
        } catch (\
Doctrine\ORM\ORMException $e) {
            
log_message("Doctrine Exception : "$e);
        }
    }


So, I do not use a YAML file for mapping but annotations directly in my Entities.

Hope this can help someone else! Have a nice day!  Big Grin
Reply


Messages In This Thread
Doctrine integration - by ScientiFist - 11-16-2019, 08:26 PM
RE: Doctrine integration - by MGatner - 11-17-2019, 04:39 AM
RE: Doctrine integration - by InsiteFX - 11-17-2019, 04:50 AM
RE: Doctrine integration - by ScientiFist - 11-17-2019, 05:58 PM
RE: Doctrine integration - by MGatner - 11-17-2019, 05:10 AM
RE: Doctrine integration - by MGatner - 11-18-2019, 05:31 AM
RE: Doctrine integration - by ScientiFist - 11-18-2019, 10:34 AM
RE: Doctrine integration - by jasonzig - 11-29-2019, 08:20 AM
RE: Doctrine integration - by MGatner - 11-18-2019, 04:24 PM
RE: Doctrine integration - by ScientiFist - 11-26-2019, 06:29 PM
RE: Doctrine integration - by ScientiFist - 11-30-2019, 07:25 AM
RE: Doctrine integration - by MGatner - 12-01-2019, 05:36 AM
RE: Doctrine integration - by troturier - 12-06-2019, 06:28 AM
RE: Doctrine integration - by ScientiFist - 03-17-2020, 07:06 PM

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.