|
|
@ -1,34 +1,77 @@ |
|
|
|
<?php |
|
|
|
<?php |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* This is a draft |
|
|
|
* This is a draft |
|
|
|
* |
|
|
|
* |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
require_once api_get_path(LIBRARY_PATH).'symfony/symfony/Component/ClassLoader/UniversalClassLoader.php'; |
|
|
|
|
|
|
|
use Doctrine\Common\ClassLoader; |
|
|
|
|
|
|
|
use Doctrine\DBAL\DriverManager; |
|
|
|
|
|
|
|
use Doctrine\ORM\Tools\Setup; |
|
|
|
use Doctrine\ORM\Tools\Setup; |
|
|
|
|
|
|
|
use Doctrine\ORM\EntityManager; |
|
|
|
|
|
|
|
use Doctrine\ORM\Configuration; |
|
|
|
|
|
|
|
|
|
|
|
require_once api_get_path(LIBRARY_PATH).'symfony/Doctrine/Common/ClassLoader.php'; |
|
|
|
function save($data) |
|
|
|
require_once api_get_path(LIBRARY_PATH).'symfony/Doctrine/ORM/Tools/Setup.php'; |
|
|
|
{ |
|
|
|
|
|
|
|
db::instance()->save($data); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
use Doctrine\ORM\EntityManager, |
|
|
|
class db |
|
|
|
Doctrine\ORM\Configuration; |
|
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* @return db |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public static function instance() |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
static $result = null; |
|
|
|
|
|
|
|
if (empty($result)) { |
|
|
|
|
|
|
|
$result = new self(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return $result; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
class db { |
|
|
|
private static function register_autoload() |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
static $has_run = false; |
|
|
|
|
|
|
|
if ($has_run) { |
|
|
|
|
|
|
|
return true; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
require_once api_get_path(LIBRARY_PATH) . 'symfony/Doctrine/ORM/Tools/Setup.php'; |
|
|
|
|
|
|
|
$directory = api_get_path(LIBRARY_PATH) . 'symfony'; |
|
|
|
|
|
|
|
//Doctrine\ORM\Tools\Setup::registerAutoloadDirectory($lib); |
|
|
|
|
|
|
|
|
|
|
|
function __construct() { |
|
|
|
if (!class_exists('Doctrine\Common\ClassLoader', false)) { |
|
|
|
global $_configuration; |
|
|
|
require_once $directory . '/doctrine/Common/ClassLoader.php'; |
|
|
|
$connectionParams = array( |
|
|
|
} |
|
|
|
'dbname' => $_configuration['main_database'], |
|
|
|
|
|
|
|
'user' => $_configuration['db_user'], |
|
|
|
$loader = new Doctrine\Common\ClassLoader('Doctrine', $directory); |
|
|
|
'password' => $_configuration['db_password'], |
|
|
|
$loader->register(); |
|
|
|
'host' => $_configuration['db_host'], |
|
|
|
|
|
|
|
'driver' => 'pdo_mysql', |
|
|
|
$loader = new Doctrine\Common\ClassLoader('Symfony\Component', $directory); |
|
|
|
); |
|
|
|
$loader->register(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$has_run = true; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected $config; |
|
|
|
|
|
|
|
protected $em; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected function __construct() |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
self::register_autoload(); |
|
|
|
|
|
|
|
$connection_parameters = $this->get_connection_parameters(); |
|
|
|
|
|
|
|
$this->config = Setup::createYAMLMetadataConfiguration(array(api_get_path(LIBRARY_PATH) . 'symfony/app_obj/metadata'), true); |
|
|
|
|
|
|
|
$this->em = EntityManager::create($connection_parameters, $this->config); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function save($data) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
$em = $this->em; |
|
|
|
|
|
|
|
$em->persist($data); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function test() |
|
|
|
|
|
|
|
{ |
|
|
|
/* $classLoader = new ClassLoader('Doctrine', api_get_path(LIBRARY_PATH).'symfony'); |
|
|
|
/* $classLoader = new ClassLoader('Doctrine', api_get_path(LIBRARY_PATH).'symfony'); |
|
|
|
$classLoader->register();*/ |
|
|
|
$classLoader->register(); */ |
|
|
|
/* $config = new Doctrine\DBAL\Configuration(); |
|
|
|
/* $config = new Doctrine\DBAL\Configuration(); |
|
|
|
|
|
|
|
|
|
|
|
$conn = DriverManager::getConnection($connectionParams, $config); |
|
|
|
$conn = DriverManager::getConnection($connectionParams, $config); |
|
|
@ -37,42 +80,163 @@ class db { |
|
|
|
$stmt = $conn->query($sql); // Simple, but has several drawbacks |
|
|
|
$stmt = $conn->query($sql); // Simple, but has several drawbacks |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
$lib = api_get_path(LIBRARY_PATH).'symfony'; |
|
|
|
// $lib = api_get_path(LIBRARY_PATH) . 'symfony'; |
|
|
|
Doctrine\ORM\Tools\Setup::registerAutoloadDirectory($lib); |
|
|
|
// Doctrine\ORM\Tools\Setup::registerAutoloadDirectory($lib); |
|
|
|
|
|
|
|
// |
|
|
|
|
|
|
|
// $config = new Configuration; |
|
|
|
|
|
|
|
// $applicationMode = 'development'; |
|
|
|
|
|
|
|
// |
|
|
|
|
|
|
|
// if ($applicationMode == "development") { |
|
|
|
|
|
|
|
// $cache = new \Doctrine\Common\Cache\ArrayCache; |
|
|
|
|
|
|
|
// } else { |
|
|
|
|
|
|
|
// $cache = new \Doctrine\Common\Cache\ApcCache; |
|
|
|
|
|
|
|
// } |
|
|
|
|
|
|
|
// $config->setMetadataCacheImpl($cache); |
|
|
|
|
|
|
|
// $driverImpl = $config->newDefaultAnnotationDriver(api_get_path(LIBRARY_PATH) . 'symfony/app_obj/entities'); |
|
|
|
|
|
|
|
// $config->setMetadataDriverImpl($driverImpl); |
|
|
|
|
|
|
|
// $config->setQueryCacheImpl($cache); |
|
|
|
|
|
|
|
// $config->setProxyDir(api_get_path(LIBRARY_PATH) . 'proxies'); |
|
|
|
|
|
|
|
// $config->setProxyNamespace('App\Proxies'); |
|
|
|
|
|
|
|
// |
|
|
|
|
|
|
|
// if ($applicationMode == "development") { |
|
|
|
|
|
|
|
// $config->setAutoGenerateProxyClasses(true); |
|
|
|
|
|
|
|
// } else { |
|
|
|
|
|
|
|
// $config->setAutoGenerateProxyClasses(false); |
|
|
|
|
|
|
|
// } |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
require_once api_get_path(LIBRARY_PATH) . 'symfony/app_obj/entities/user.php'; |
|
|
|
|
|
|
|
$user = new User(); |
|
|
|
|
|
|
|
$user->setUsername('salut'); |
|
|
|
|
|
|
|
$em->persist($user); |
|
|
|
|
|
|
|
$em->flush(); |
|
|
|
|
|
|
|
echo "Created User with ID " . $user->getId() . "\n"; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
$config = new Configuration; |
|
|
|
/** |
|
|
|
$applicationMode = 'development'; |
|
|
|
* |
|
|
|
|
|
|
|
* @return EntityManager The created EntityManager. |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public function em() |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
return $this->em; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if ($applicationMode == "development") { |
|
|
|
/** |
|
|
|
$cache = new \Doctrine\Common\Cache\ArrayCache; |
|
|
|
* Pathes?? do we put everything in one place or in several places? |
|
|
|
} else { |
|
|
|
* @return type |
|
|
|
$cache = new \Doctrine\Common\Cache\ApcCache; |
|
|
|
*/ |
|
|
|
|
|
|
|
public function get_model_path() |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
$result = __DIR__ . '/../model'; |
|
|
|
|
|
|
|
$result = realpath($result); |
|
|
|
|
|
|
|
return $result; |
|
|
|
} |
|
|
|
} |
|
|
|
$config->setMetadataCacheImpl($cache); |
|
|
|
|
|
|
|
$driverImpl = $config->newDefaultAnnotationDriver(api_get_path(LIBRARY_PATH).'symfony/app_obj/entities'); |
|
|
|
|
|
|
|
$config->setMetadataDriverImpl($driverImpl); |
|
|
|
|
|
|
|
$config->setQueryCacheImpl($cache); |
|
|
|
|
|
|
|
$config->setProxyDir(api_get_path(LIBRARY_PATH).'proxies'); |
|
|
|
|
|
|
|
$config->setProxyNamespace('App\Proxies'); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ($applicationMode == "development") { |
|
|
|
public function is_production() |
|
|
|
$config->setAutoGenerateProxyClasses(true); |
|
|
|
{ |
|
|
|
|
|
|
|
return Chamilo::is_production_server(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function is_dev() |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
return !Chamilo::is_production_server(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Reverse engineering of the model from the database structure. |
|
|
|
|
|
|
|
* Write result to the entity folder |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* WARNING THIS WILL OVERWRITE EXISTING MODEL. |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @return boolean |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public function generate_model() |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
if (!$this->is_dev()) { |
|
|
|
|
|
|
|
return false; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$model_path = $this->get_model_path(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$connection_parameters = $this->get_connection_parameters(); |
|
|
|
|
|
|
|
$connection = Doctrine\DBAL\DriverManager::getConnection($connection_parameters); |
|
|
|
|
|
|
|
$platform = $connection->getDatabasePlatform(); |
|
|
|
|
|
|
|
$platform->registerDoctrineTypeMapping('enum', 'string'); |
|
|
|
|
|
|
|
$platform->registerDoctrineTypeMapping('set', 'string'); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$config = Setup::createConfiguration($this->is_dev()); |
|
|
|
|
|
|
|
$config->setMetadataDriverImpl(new Doctrine\ORM\Mapping\Driver\DatabaseDriver(new Doctrine\DBAL\Schema\MySqlSchemaManager($connection))); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$em = EntityManager::create($connection, $config); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$cmf = new Doctrine\ORM\Tools\DisconnectedClassMetadataFactory(); |
|
|
|
|
|
|
|
$cmf->setEntityManager($em); |
|
|
|
|
|
|
|
$metadatas = $cmf->getAllMetadata(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
foreach ($metadatas as $metadata) { |
|
|
|
|
|
|
|
echo sprintf('Processing entity "<info>%s</info>"', $metadata->name) . '<br/>'; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$generator = new EntityGenerator(); |
|
|
|
|
|
|
|
$generator->setGenerateAnnotations(false); |
|
|
|
|
|
|
|
$generator->setGenerateStubMethods(true); |
|
|
|
|
|
|
|
$generator->setRegenerateEntityIfExists(true); |
|
|
|
|
|
|
|
$generator->setUpdateEntityIfExists(false); |
|
|
|
|
|
|
|
$generator->setExtension('.class.php'); |
|
|
|
|
|
|
|
$generator->setNumSpaces(4); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Generating Entities |
|
|
|
|
|
|
|
$generator->generate($metadatas, $model_path); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$exporter = new \Doctrine\ORM\Tools\Export\Driver\YamlExporter(); |
|
|
|
|
|
|
|
$exporter->setOutputDir($model_path . '/mapping'); |
|
|
|
|
|
|
|
$exporter->setMetadata($metadatas); |
|
|
|
|
|
|
|
$exporter->export(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function update_schema($paths, $save_mode) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
$is_dev = $this->is_dev(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$ext = explode('.', $path); |
|
|
|
|
|
|
|
$ext = end($ext); |
|
|
|
|
|
|
|
if ($ext == 'yml') { |
|
|
|
|
|
|
|
$config = Setup::createYAMLMetadataConfiguration($paths, $is_dev); |
|
|
|
|
|
|
|
} else if ($ext == 'xml') { |
|
|
|
|
|
|
|
$config = Setup::createXMLMetadataConfiguration($paths, $is_dev); |
|
|
|
|
|
|
|
} else if ($ext == 'php') { |
|
|
|
|
|
|
|
$config = Setup::createAnnotationMetadataConfiguration($paths, $is_dev); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
$config->setAutoGenerateProxyClasses(false); |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
$config = Setup::createYAMLMetadataConfiguration(array(api_get_path(LIBRARY_PATH).'symfony/app_obj/metadata'), true); |
|
|
|
$em = $this->em; |
|
|
|
|
|
|
|
|
|
|
|
$em = EntityManager::create($connectionParams, $config); |
|
|
|
$cmf = new Doctrine\ORM\Tools\DisconnectedClassMetadataFactory(); |
|
|
|
|
|
|
|
$cmf->setEntityManager($em); |
|
|
|
|
|
|
|
$classes = $cmf->getAllMetadata(); |
|
|
|
|
|
|
|
|
|
|
|
require_once api_get_path(LIBRARY_PATH).'symfony/app_obj/entities/user.php'; |
|
|
|
$tool = new Doctrine\ORM\Tools\SchemaTool($em); |
|
|
|
|
|
|
|
return $tool->updateSchema($classes, $save_mode); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
$user = new User(); |
|
|
|
protected function get_config() |
|
|
|
$user->setUsername('salut'); |
|
|
|
{ |
|
|
|
$em->persist($user); |
|
|
|
return $this->config; |
|
|
|
$em->flush(); |
|
|
|
|
|
|
|
echo "Created User with ID " . $user->getId() . "\n"; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected function get_connection_parameters() |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
global $_configuration; |
|
|
|
|
|
|
|
$result = array( |
|
|
|
|
|
|
|
'dbname' => $_configuration['main_database'], |
|
|
|
|
|
|
|
'user' => $_configuration['db_user'], |
|
|
|
|
|
|
|
'password' => $_configuration['db_password'], |
|
|
|
|
|
|
|
'host' => $_configuration['db_host'], |
|
|
|
|
|
|
|
'driver' => 'pdo_mysql', |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
return $result; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|