console-config.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. $config = new \Doctrine\ORM\Configuration();
  4. $config->setMetadataCacheImpl(new \Doctrine\Common\Cache\ArrayCache);
  5. use Doctrine\Common\Annotations\AnnotationReader;
  6. use Doctrine\Common\Annotations\AnnotationRegistry;
  7. $sysPath = __DIR__."/";
  8. AnnotationRegistry::registerFile($sysPath."vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/Driver/DoctrineAnnotations.php");
  9. $reader = new AnnotationReader();
  10. $driverImpl = new \Doctrine\ORM\Mapping\Driver\AnnotationDriver($reader, array($sysPath."tests/doctrine_console/mapping"));
  11. $config->setMetadataDriverImpl($driverImpl);
  12. $config->setProxyDir(__DIR__ . '/Proxies');
  13. $config->setProxyNamespace('Proxies');
  14. $courseList = CourseManager::get_real_course_list();
  15. $app['chamilo.log'] = $app['sys_log_path'].'/chamilo-cli.log';
  16. // Loading db connections
  17. $connectionOptions = array();
  18. if (!empty($courseList)) {
  19. $dbPrefix = isset($_configuration['db_prefix']) && !empty($_configuration['db_prefix']) ? $_configuration['db_prefix'].Database::get_database_glue() : null;
  20. foreach ($courseList as $course) {
  21. $connectionOptions['_chamilo_course_'.$course['db_name']] = array(
  22. 'driver' => 'pdo_mysql',
  23. 'dbname' => $dbPrefix.$course['db_name'],
  24. 'user' => $_configuration['db_user'],
  25. 'password' => $_configuration['db_password'],
  26. 'host' => $_configuration['db_host'],
  27. );
  28. }
  29. }
  30. if (isset($_configuration['main_database'])) {
  31. $connectionOptions['main_database'] = array(
  32. 'driver' => 'pdo_mysql',
  33. 'dbname' => $_configuration['main_database'],
  34. 'user' => $_configuration['db_user'],
  35. 'password' => $_configuration['db_password'],
  36. 'host' => $_configuration['db_host'],
  37. );
  38. }
  39. if (isset($_configuration['statistics_database'])) {
  40. $connectionOptions['statistics_database'] = array(
  41. 'driver' => 'pdo_mysql',
  42. 'dbname' => $_configuration['statistics_database'],
  43. 'user' => $_configuration['db_user'],
  44. 'password' => $_configuration['db_password'],
  45. 'host' => $_configuration['db_host'],
  46. );
  47. } else {
  48. if (isset($_configuration['main_database'])) {
  49. $connectionOptions['statistics_database'] = $connectionOptions['main_database'];
  50. }
  51. }
  52. if (isset($_configuration['user_personal_database'])) {
  53. $connectionOptions['user_personal_database'] = array(
  54. 'driver' => 'pdo_mysql',
  55. 'dbname' => $_configuration['user_personal_database'],
  56. 'user' => $_configuration['db_user'],
  57. 'password' => $_configuration['db_password'],
  58. 'host' => $_configuration['db_host'],
  59. );
  60. } else {
  61. if (isset($_configuration['main_database'])) {
  62. $connectionOptions['user_personal_database'] = $connectionOptions['main_database'];
  63. }
  64. }
  65. $defaultConnection = array(
  66. 'driver' => 'pdo_mysql'
  67. );
  68. if (isset($_configuration['main_database'])) {
  69. $defaultConnection = array(
  70. 'driver' => 'pdo_mysql',
  71. 'dbname' => $_configuration['main_database'],
  72. 'user' => $_configuration['db_user'],
  73. 'password' => $_configuration['db_password'],
  74. 'host' => $_configuration['db_host'],
  75. );
  76. }
  77. $em = \Doctrine\ORM\EntityManager::create($defaultConnection, $config);
  78. //Fixes some errors
  79. $platform = $em->getConnection()->getDatabasePlatform();
  80. $platform->registerDoctrineTypeMapping('enum', 'string');
  81. $platform->registerDoctrineTypeMapping('set', 'string');
  82. $helpers = array(
  83. 'db' => new \Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper($em->getConnection()),
  84. 'em' => new \Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper($em),
  85. 'configuration' => new \Chash\Helpers\ConfigurationHelper()
  86. );
  87. use Doctrine\DBAL\DriverManager;
  88. $multipleEM = array();
  89. foreach ($connectionOptions as $name => $connection) {
  90. $em = \Doctrine\ORM\EntityManager::create($connection, $config);
  91. //$helpers[$name] = new \Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper($em);
  92. $helpers[$name] = new \Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper($em->getConnection());
  93. }
  94. /*
  95. To generate doctrine2 entities you must:
  96. cd /var/www/chamilo11/tests/doctrine_console
  97. Delete old mappings/entities
  98. sudo rm -R mapping generated repository
  99. Creating the mapping from the DB
  100. sudo mkdir mapping generated repository
  101. You can add a Namespace if you want to with: --namespace "Entity"
  102. sudo php5 doctrine.php orm:convert-mapping --force --from-database --namespace "Entity" annotation mapping
  103. 1. Generate entities
  104. sudo php5 doctrine.php orm:generate-entities --generate-annotations="true" generated
  105. Validate schema
  106. sudo php5 doctrine.php orm:validate-schema -v
  107. Move generated files in a chamilo folder:
  108. sudo rm -R main/inc/Entity/*
  109. mkdir main/inc/Entity
  110. cp -R tests/doctrine_console/generated/* main/inc/Entity
  111. fixes \ORM bug see http://redgreenrefactor.blogsite.org/php/code-first-approaching-php-with-doctrine-2-2-1-and-composer/
  112. cd main/inc/Entity
  113. sed -i 's/@ORM\\/@/g' *.php
  114. For tests
  115. php5 tests/doctrine_console/doctrine.php orm:generate-entities --generate-annotations="true" main/inc/Entity
  116. Then autoload classes with composer
  117. sudo php5 composer.phar update or sudo composer.phar update
  118. 2. Migrations
  119. a. Generate empty migration file
  120. cd /var/www/chamilo11/tests/doctrine_console
  121. php5 doctrine.php migrations:generate
  122. b. Check status
  123. php5 doctrine.php migrations:status
  124. c. Check sql
  125. php5 doctrine.php migrations:migrate --dry-run
  126. d. execute migration
  127. php5 doctrine.php migrations:migrate
  128. e. Revert migrations
  129. php5 doctrine.php migrations:migrate 0
  130. http://docs.doctrine-project.org/projects/doctrine-migrations/en/latest/reference/managing_migrations.html
  131. */