cli-config.php 6.2 KB

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