parent
cb3175325e
commit
21f90b6db3
@ -0,0 +1,185 @@ |
||||
<?php |
||||
|
||||
$config = new \Doctrine\ORM\Configuration(); |
||||
$config->setMetadataCacheImpl(new \Doctrine\Common\Cache\ArrayCache); |
||||
|
||||
use Doctrine\Common\Annotations\AnnotationReader; |
||||
use Doctrine\Common\Annotations\AnnotationRegistry; |
||||
use Symfony\Component\Yaml\Parser; |
||||
|
||||
AnnotationRegistry::registerFile(api_get_path(SYS_PATH)."vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/Driver/DoctrineAnnotations.php"); |
||||
$reader = new AnnotationReader(); |
||||
|
||||
$driverImpl = new \Doctrine\ORM\Mapping\Driver\AnnotationDriver($reader, array(api_get_path(SYS_PATH)."tests/doctrine_console/mapping")); |
||||
|
||||
$config->setMetadataDriverImpl($driverImpl); |
||||
$config->setProxyDir(__DIR__ . '/Proxies'); |
||||
$config->setProxyNamespace('Proxies'); |
||||
|
||||
$courseList = CourseManager::get_real_course_list(); |
||||
//$courseList = array(); |
||||
|
||||
$configurationPath = api_get_path(SYS_PATH).'main/inc/conf/'; |
||||
$newConfigurationFile = $configurationPath.'configuration.yml'; |
||||
|
||||
if (is_file($newConfigurationFile) && file_exists($newConfigurationFile)) { |
||||
$yaml = new Parser(); |
||||
$_configuration = $yaml->parse(file_get_contents($newConfigurationFile)); |
||||
} |
||||
|
||||
$connectionOptions = array(); |
||||
|
||||
if (!empty($courseList)) { |
||||
|
||||
$dbPrefix = isset($_configuration['db_prefix']) && !empty($_configuration['db_prefix']) ? $_configuration['db_prefix'].$_configuration['db_glue'] : null; |
||||
foreach ($courseList as $course) { |
||||
$connectionOptions['_chamilo_course_'.$course['db_name']] = array( |
||||
'driver' => 'pdo_mysql', |
||||
'dbname' => $dbPrefix.$course['db_name'], |
||||
'user' => $_configuration['db_user'], |
||||
'password' => $_configuration['db_password'], |
||||
'host' => $_configuration['db_host'], |
||||
); |
||||
} |
||||
} |
||||
|
||||
if (isset($_configuration['main_database'])) { |
||||
$connectionOptions['main_database'] = array( |
||||
'driver' => 'pdo_mysql', |
||||
'dbname' => $_configuration['main_database'], |
||||
'user' => $_configuration['db_user'], |
||||
'password' => $_configuration['db_password'], |
||||
'host' => $_configuration['db_host'], |
||||
); |
||||
} |
||||
|
||||
if (isset($_configuration['statistics_database'])) { |
||||
$connectionOptions['statistics_database'] = array( |
||||
'driver' => 'pdo_mysql', |
||||
'dbname' => $_configuration['statistics_database'], |
||||
'user' => $_configuration['db_user'], |
||||
'password' => $_configuration['db_password'], |
||||
'host' => $_configuration['db_host'], |
||||
); |
||||
} else { |
||||
if (isset($_configuration['main_database'])) { |
||||
$connectionOptions['statistics_database'] = $connectionOptions['main_database']; |
||||
} |
||||
} |
||||
|
||||
if (isset($_configuration['user_personal_database'])) { |
||||
$connectionOptions['user_personal_database'] = array( |
||||
'driver' => 'pdo_mysql', |
||||
'dbname' => $_configuration['user_personal_database'], |
||||
'user' => $_configuration['db_user'], |
||||
'password' => $_configuration['db_password'], |
||||
'host' => $_configuration['db_host'], |
||||
); |
||||
} else { |
||||
if (isset($_configuration['main_database'])) { |
||||
$connectionOptions['user_personal_database'] = $connectionOptions['main_database']; |
||||
} |
||||
} |
||||
|
||||
$defaultConnection = array( |
||||
'driver' => 'pdo_mysql' |
||||
); |
||||
|
||||
if (isset($_configuration['main_database'])) { |
||||
$defaultConnection = array( |
||||
'driver' => 'pdo_mysql', |
||||
'dbname' => $_configuration['main_database'], |
||||
'user' => $_configuration['db_user'], |
||||
'password' => $_configuration['db_password'], |
||||
'host' => $_configuration['db_host'], |
||||
); |
||||
} |
||||
|
||||
$em = \Doctrine\ORM\EntityManager::create($defaultConnection, $config); |
||||
|
||||
//Fixes some errors |
||||
$platform = $em->getConnection()->getDatabasePlatform(); |
||||
$platform->registerDoctrineTypeMapping('enum', 'string'); |
||||
$platform->registerDoctrineTypeMapping('set', 'string'); |
||||
|
||||
$helpers = array( |
||||
'db' => new \Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper($em->getConnection()), |
||||
'em' => new \Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper($em), |
||||
'configuration' => new \Chash\Helpers\ConfigurationHelper() |
||||
); |
||||
|
||||
use Doctrine\DBAL\DriverManager; |
||||
$multipleEM = array(); |
||||
foreach ($connectionOptions as $name => $connection) { |
||||
$em = \Doctrine\ORM\EntityManager::create($connection, $config); |
||||
//$helpers[$name] = new \Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper($em); |
||||
$helpers[$name] = new \Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper($em->getConnection()); |
||||
} |
||||
|
||||
/* |
||||
To generate doctrine2 entities you must: |
||||
|
||||
cd /var/www/chamilo11/tests/doctrine_console |
||||
|
||||
Delete old mappings/entities |
||||
|
||||
sudo rm -R mapping generated repository |
||||
|
||||
Creating the mapping from the DB |
||||
|
||||
sudo mkdir mapping generated repository |
||||
|
||||
You can add a Namespace if you want to with: --namespace "Entity" |
||||
sudo php5 doctrine.php orm:convert-mapping --force --from-database --namespace "Entity" annotation mapping |
||||
|
||||
|
||||
1. Generate entities |
||||
|
||||
sudo php5 doctrine.php orm:generate-entities --generate-annotations="true" generated |
||||
|
||||
Validate schema |
||||
sudo php5 doctrine.php orm:validate-schema -v |
||||
|
||||
Move generated files in a chamilo folder: |
||||
|
||||
sudo rm -R main/inc/Entity/* |
||||
mkdir main/inc/Entity |
||||
|
||||
cp -R tests/doctrine_console/generated/* main/inc/Entity |
||||
|
||||
fixes \ORM bug see http://redgreenrefactor.blogsite.org/php/code-first-approaching-php-with-doctrine-2-2-1-and-composer/ |
||||
cd main/inc/Entity |
||||
|
||||
sed -i 's/@ORM\\/@/g' *.php |
||||
|
||||
For tests |
||||
php5 tests/doctrine_console/doctrine.php orm:generate-entities --generate-annotations="true" main/inc/Entity |
||||
|
||||
Then autoload classes with composer |
||||
sudo php5 composer.phar update or sudo composer.phar update |
||||
|
||||
2. Migrations |
||||
|
||||
a. Generate empty migration file |
||||
cd /var/www/chamilo11/tests/doctrine_console |
||||
|
||||
php5 doctrine.php migrations:generate |
||||
|
||||
b. Check status |
||||
|
||||
php5 doctrine.php migrations:status |
||||
|
||||
c. Check sql |
||||
php5 doctrine.php migrations:migrate --dry-run |
||||
|
||||
d. execute migration |
||||
php5 doctrine.php migrations:migrate |
||||
|
||||
e. Revert migrations |
||||
php5 doctrine.php migrations:migrate 0 |
||||
|
||||
|
||||
http://docs.doctrine-project.org/projects/doctrine-migrations/en/latest/reference/managing_migrations.html |
||||
|
||||
*/ |
||||
|
||||
@ -0,0 +1,67 @@ |
||||
#!/usr/bin/env php |
||||
<?php |
||||
set_time_limit(0); |
||||
|
||||
require_once dirname(__FILE__).'/../main/inc/global.inc.php'; |
||||
|
||||
// Variable $helperSet is defined inside cli-config.php |
||||
require __DIR__ . '/config/console-config.php'; |
||||
|
||||
//$cli = new \Symfony\Component\Console\Application('Doctrine Command Line Interface', Doctrine\Common\Version::VERSION); |
||||
$cli = new \Symfony\Component\Console\Application('Chamilo Command Line Interface'); |
||||
$cli->setCatchExceptions(true); |
||||
|
||||
$helperSet = $cli->getHelperSet(); |
||||
foreach ($helpers as $name => $helper) { |
||||
$helperSet->set($helper, $name); |
||||
} |
||||
|
||||
$cli->addCommands(array( |
||||
// DBAL Commands |
||||
new \Doctrine\DBAL\Tools\Console\Command\RunSqlCommand(), |
||||
new \Doctrine\DBAL\Tools\Console\Command\ImportCommand(), |
||||
|
||||
// ORM Commands |
||||
new \Doctrine\ORM\Tools\Console\Command\ClearCache\MetadataCommand(), |
||||
new \Doctrine\ORM\Tools\Console\Command\ClearCache\ResultCommand(), |
||||
new \Doctrine\ORM\Tools\Console\Command\ClearCache\QueryCommand(), |
||||
new \Doctrine\ORM\Tools\Console\Command\SchemaTool\CreateCommand(), |
||||
new \Doctrine\ORM\Tools\Console\Command\SchemaTool\UpdateCommand(), |
||||
new \Doctrine\ORM\Tools\Console\Command\SchemaTool\DropCommand(), |
||||
new \Doctrine\ORM\Tools\Console\Command\EnsureProductionSettingsCommand(), |
||||
new \Doctrine\ORM\Tools\Console\Command\ConvertDoctrine1SchemaCommand(), |
||||
new \Doctrine\ORM\Tools\Console\Command\GenerateRepositoriesCommand(), |
||||
new \Doctrine\ORM\Tools\Console\Command\GenerateEntitiesCommand(), |
||||
new \Doctrine\ORM\Tools\Console\Command\GenerateProxiesCommand(), |
||||
new \Doctrine\ORM\Tools\Console\Command\ConvertMappingCommand(), |
||||
new \Doctrine\ORM\Tools\Console\Command\RunDqlCommand(), |
||||
new \Doctrine\ORM\Tools\Console\Command\ValidateSchemaCommand(), |
||||
|
||||
// Migrations Commands |
||||
new \Doctrine\DBAL\Migrations\Tools\Console\Command\DiffCommand(), |
||||
new \Doctrine\DBAL\Migrations\Tools\Console\Command\ExecuteCommand(), |
||||
new \Doctrine\DBAL\Migrations\Tools\Console\Command\GenerateCommand(), |
||||
new \Doctrine\DBAL\Migrations\Tools\Console\Command\MigrateCommand(), |
||||
new \Doctrine\DBAL\Migrations\Tools\Console\Command\StatusCommand(), |
||||
new \Doctrine\DBAL\Migrations\Tools\Console\Command\VersionCommand(), |
||||
|
||||
//Chamilo commands |
||||
new ChamiloLMS\Command\Database\UpgradeCommand(), |
||||
new ChamiloLMS\Command\Database\InstallCommand(), |
||||
new ChamiloLMS\Command\Database\StatusCommand(), |
||||
new ChamiloLMS\Command\Database\SetupCommand(), |
||||
|
||||
//Chash commands |
||||
new Chash\Command\Database\RunSQLCommand(), |
||||
new Chash\Command\Database\DumpCommand(), |
||||
new Chash\Command\Database\RestoreCommand(), |
||||
new Chash\Command\Database\SQLCountCommand(), |
||||
new Chash\Command\Database\FullBackupCommand(), |
||||
new Chash\Command\Database\DropDatabaseCommand(), |
||||
new Chash\Command\Files\CleanTempFolderCommand(), |
||||
new Chash\Command\Files\CleanConfigFiles(), |
||||
new Chash\Command\Translation\ExportLanguageCommand(), |
||||
new Chash\Command\Translation\ImportLanguageCommand() |
||||
|
||||
)); |
||||
$cli->run(); |
||||
Loading…
Reference in new issue