db.class.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. <?php
  2. /**
  3. * This is a draft
  4. *
  5. */
  6. use Doctrine\ORM\Tools\Setup;
  7. use Doctrine\ORM\EntityManager;
  8. use Doctrine\ORM\Configuration;
  9. use Tools\EntityGenerator;
  10. use Tools\EntityRepositoryGenerator;
  11. use Doctrine\Common\Util\Inflector;
  12. use Doctrine\ORM\Mapping\ClassMetadataInfo;
  13. function save($data)
  14. {
  15. db::instance()->save($data);
  16. }
  17. /**
  18. *
  19. * @license see /license.txt
  20. */
  21. class db
  22. {
  23. /**
  24. * @return db
  25. */
  26. public static function instance()
  27. {
  28. static $result = null;
  29. if (empty($result)) {
  30. $result = new self();
  31. }
  32. return $result;
  33. }
  34. private static function register_autoload()
  35. {
  36. static $has_run = false;
  37. if ($has_run) {
  38. return true;
  39. }
  40. require_once api_get_path(LIBRARY_PATH) . 'symfony/Doctrine/ORM/Tools/Setup.php';
  41. $directory = api_get_path(LIBRARY_PATH) . 'symfony';
  42. if (!class_exists('Doctrine\Common\ClassLoader', false)) {
  43. require_once $directory . '/doctrine/Common/ClassLoader.php';
  44. }
  45. $loader = new Doctrine\Common\ClassLoader('Doctrine', $directory);
  46. $loader->register();
  47. $loader = new Doctrine\Common\ClassLoader('Symfony\Component', $directory);
  48. $loader->register();
  49. $has_run = true;
  50. }
  51. protected $config;
  52. protected $em;
  53. protected function __construct()
  54. {
  55. self::register_autoload();
  56. $connection_parameters = $this->get_connection_parameters();
  57. $this->config = Setup::createYAMLMetadataConfiguration(array($this->get_entity_path() . '/mapping'), true);
  58. $this->em = EntityManager::create($connection_parameters, $this->config);
  59. }
  60. public function save($data)
  61. {
  62. $em = $this->em;
  63. $em->persist($data);
  64. $em->flush();
  65. }
  66. public function remove($data)
  67. {
  68. $em = $this->em;
  69. $em->remove($data);
  70. $em->flush();
  71. }
  72. public function flush($data = null)
  73. {
  74. $em = $this->em;
  75. $em->flush($data);
  76. }
  77. function test()
  78. {
  79. $_SESSION['_real_cid'] = 2;
  80. // $repo = Entity\User::repository();
  81. // $user = Entity\User::create();
  82. // $user->set_username('salut1');
  83. // $user->set_password('salut');
  84. // $user->set_status(1);
  85. // $user->set_chatcall_user_id(1);
  86. // $user->set_chatcall_date(new DateTime());
  87. // $user->set_chatcall_text('');
  88. // $user->set_registration_date(new DateTime());
  89. // $user->set_expiration_date(new DateTime());
  90. // $user->set_active(true);
  91. // $user->set_hr_dept_id(0);
  92. // $em = $this->em();
  93. // $em->persist($user);
  94. // $em->flush();
  95. // echo "Created User with ID " . $user->get_user_id() . "\n";
  96. // $user = $repo->find(3);
  97. // $this->remove($user);
  98. // $this->flush();
  99. $doc = \Entity\Document::create();
  100. $doc->set_path('path');
  101. $doc->set_title('title');
  102. //$doc->set_c_id(1);
  103. $doc->set_comment('comment');
  104. $doc->set_size(0);
  105. $doc->set_session_id(0);
  106. $doc->set_filetype('dd');
  107. $doc->set_readonly(false);
  108. //$repo = \Entity\Document::repository();
  109. //$id = $repo->next_id($doc);
  110. //echo "next id: $id <br/>";
  111. //$doc->set_id($id);
  112. $this->save($doc);
  113. $this->flush();
  114. }
  115. /**
  116. *
  117. * @return EntityManager The created EntityManager.
  118. */
  119. public function em()
  120. {
  121. return $this->em;
  122. }
  123. /**
  124. *
  125. * @param string $entity_name
  126. * @return EntityRepository
  127. */
  128. public function get_repository($entity_name)
  129. {
  130. return $this->em()->getRepository($entity_name);
  131. }
  132. /**
  133. *
  134. * @return type
  135. */
  136. public function get_entity_path()
  137. {
  138. $result = __DIR__ . '/../entity';
  139. $result = realpath($result);
  140. return $result;
  141. }
  142. public function is_production()
  143. {
  144. return Chamilo::is_production_server();
  145. }
  146. public function is_dev()
  147. {
  148. return !Chamilo::is_production_server();
  149. }
  150. /**
  151. * Reverse engineering of the model from the database structure.
  152. * Write result to the entity folder
  153. *
  154. * WARNING THIS WILL OVERWRITE EXISTING MODEL.
  155. *
  156. * @return boolean
  157. */
  158. public function generate_model()
  159. {
  160. if (!$this->is_dev()) {
  161. return false;
  162. }
  163. $root = $this->get_entity_path();
  164. $connection_parameters = $this->get_connection_parameters();
  165. $connection = Doctrine\DBAL\DriverManager::getConnection($connection_parameters);
  166. $platform = $connection->getDatabasePlatform();
  167. $platform->registerDoctrineTypeMapping('enum', 'string');
  168. $platform->registerDoctrineTypeMapping('set', 'string');
  169. $config = Setup::createConfiguration($this->is_dev());
  170. $config->setMetadataDriverImpl(new Doctrine\ORM\Mapping\Driver\DatabaseDriver(new Doctrine\DBAL\Schema\MySqlSchemaManager($connection)));
  171. $em = EntityManager::create($connection, $config);
  172. $cmf = new Doctrine\ORM\Tools\DisconnectedClassMetadataFactory();
  173. $cmf->setEntityManager($em);
  174. $metadatas = $cmf->getAllMetadata();
  175. $repo_factory = new EntityRepositoryGenerator();
  176. $course = null;
  177. foreach ($metadatas as $metadata) {
  178. $n = strtolower($metadata->name);
  179. if ($n == 'course') {
  180. $course = $metadata;
  181. break;
  182. }
  183. }
  184. foreach ($metadatas as $metadata) {
  185. echo sprintf('Processing entity "<info>%s</info>"', $metadata->name) . '<br/>';
  186. foreach ($metadata->identifier as $key => $value) {
  187. //$mapping = $metadata->fieldMappings[$value];
  188. $metadata->identifier[$key] = Inflector::tableize($value);
  189. }
  190. $fields = array();
  191. foreach ($metadata->fieldMappings as $fieldMapping) {
  192. $name = Inflector::tableize($fieldMapping['fieldName']);
  193. $fieldMapping['fieldName'] = $name;
  194. $fields[$name] = $fieldMapping;
  195. }
  196. $metadata->fieldMappings = $fields;
  197. $n = $metadata->name;
  198. if ($n == 'CDocument') {
  199. $i = 1;
  200. }
  201. $name = $metadata->name;
  202. $name = Inflector::tableize($name);
  203. $is_course_table = (strpos($name, 'c_') === 0);
  204. if ($is_course_table) {
  205. $name = substr($name, 2, strlen($name) - 2);
  206. }
  207. //$metadata->namespace = 'Entity';
  208. $metadata->customRepositoryClassName = 'Entity\\Repository\\' . Inflector::classify($name) . 'Repository';
  209. //if(is_course_table){
  210. $metadata->name = 'Entity\\' . Inflector::classify($name);
  211. $metadata->lifecycleCallbacks['prePersist'] = array('before_save');
  212. //}
  213. //$metadata->rootEntityName = Inflector::classify($name);
  214. if ($is_course_table) {
  215. foreach ($metadata->fieldMappings as $mapping) {
  216. $name = $mapping['columnName'];
  217. $is_id = isset($mapping['id']) ? $mapping['id'] : false;
  218. if ($name != 'c_id' && $is_id) {
  219. }
  220. }
  221. }
  222. if ($is_course_table) {
  223. $metadata->is_course_table = true;
  224. // $mapping = array();
  225. // $mapping['cascade'] = array();
  226. // $mapping['joinColumns'][0] = array('name' => 'c_id', 'referencedColumnName' => 'id');
  227. // $mapping['sourceToTargetKeyColumns']['c_id'] = 'id';
  228. // $mapping['joinColumnFieldNames']['c_id'] = 'c_id';
  229. // $mapping['targetToSourceKeyColumns']['id'] = 'c_id';
  230. // $mapping['id'] = 1;
  231. // $mapping['isOwningSide'] = 0;
  232. // $mapping['isCascadeRemove'] = 0;
  233. // $mapping['isCascadePersist'] = 0;
  234. // $mapping['isCascadeRefresh'] = 0;
  235. // $mapping['isCascadeMerge'] = 0;
  236. // $mapping['isCascadeDetach'] = 0;
  237. // $mapping['orphanRemoval'] = 0;
  238. // $mapping['type'] = ClassMetadataInfo::MANY_TO_ONE;
  239. // $mapping['fetch'] = ClassMetadataInfo::FETCH_LAZY;
  240. // $mapping['fieldName'] = 'course';
  241. // $mapping['targetEntity'] = 'Entity\\Course';
  242. // $mapping['sourceEntity'] = $metadata->name;
  243. //
  244. // $metadata->associationMappings['course'] = $mapping;
  245. // $metadata->identifier['course'];
  246. // unset($metadata->identifier['c_id']);
  247. // unset($metadata->fieldMappings['c_id']);
  248. // $mapping = array();
  249. // $mapping['cascade'] = array();
  250. // $mapping['joinColumns'][0] = array('name' => 'id', 'referencedColumnName' => 'c_id');
  251. // $mapping['sourceToTargetKeyColumns']['id'] = 'c_id';
  252. // $mapping['joinColumnFieldNames']['id'] = 'id';
  253. // $mapping['targetToSourceKeyColumns']['c_id'] = 'id';
  254. // $mapping['id'] = 1;
  255. // $mapping['isOwningSide'] = 1;
  256. // $mapping['isCascadeRemove'] = 0;
  257. // $mapping['isCascadePersist'] = 0;
  258. // $mapping['isCascadeRefresh'] = 0;
  259. // $mapping['isCascadeMerge'] = 0;
  260. // $mapping['isCascadeDetach'] = 0;
  261. // $mapping['orphanRemoval'] = 0;
  262. // $mapping['type'] = ClassMetadataInfo::ONE_TO_MANY;
  263. // $mapping['fetch'] = ClassMetadataInfo::FETCH_LAZY;
  264. // $name = explode('\\' ,$metadata->name);
  265. // $name = end($name);
  266. // $name = Inflector::tableize($name);
  267. // $mapping['fieldName'] = $name;
  268. // $mapping['targetEntity'] = $metadata->name;
  269. // $mapping['sourceEntity'] = 'Entity\\Course';
  270. // $course->associationMappings[$name] = $mapping;
  271. }
  272. $metadata->class_to_extend = $is_course_table ? 'CourseEntity' : 'Entity';
  273. $repo_factory->writeEntityRepositoryClass($metadata->name, $root . '\\repository\\');
  274. }
  275. $generator = new EntityGenerator();
  276. $generator->setClassToExtend('Entity');
  277. $generator->setGenerateAnnotations(false);
  278. $generator->setGenerateStubMethods(true);
  279. $generator->setRegenerateEntityIfExists(false);
  280. $generator->setUpdateEntityIfExists(false);
  281. $generator->setBackupExisting(false);
  282. $generator->setExtension('.class.php');
  283. $generator->setNumSpaces(4);
  284. // Generating Entities
  285. $generator->generate($metadatas, $root);
  286. $exporter = new \Doctrine\ORM\Tools\Export\Driver\YamlExporter();
  287. $exporter->setOutputDir($root . '/mapping');
  288. foreach ($metadatas as $metadata) {
  289. echo $metadata->name . '<br/>';
  290. try {
  291. $exporter->setMetadata(array($metadata));
  292. $exporter->export();
  293. } catch (Exception $e) {
  294. echo $e->getMessage();
  295. }
  296. }
  297. }
  298. public function update_schema($paths, $save_mode)
  299. {
  300. // $is_dev = $this->is_dev();
  301. //
  302. // $ext = explode('.', $path);
  303. // $ext = end($ext);
  304. // if ($ext == 'yml') {
  305. // $config = Setup::createYAMLMetadataConfiguration($paths, $is_dev);
  306. // } else if ($ext == 'xml') {
  307. // $config = Setup::createXMLMetadataConfiguration($paths, $is_dev);
  308. // } else if ($ext == 'php') {
  309. // $config = Setup::createAnnotationMetadataConfiguration($paths, $is_dev);
  310. // } else {
  311. // return false;
  312. // }
  313. //
  314. // $em = $this->em;
  315. //
  316. // $cmf = new Doctrine\ORM\Tools\DisconnectedClassMetadataFactory();
  317. // $cmf->setEntityManager($em);
  318. // $classes = $cmf->getAllMetadata();
  319. //
  320. // $tool = new Doctrine\ORM\Tools\SchemaTool($em);
  321. // return $tool->updateSchema($classes, $save_mode);
  322. }
  323. protected function get_config()
  324. {
  325. return $this->config;
  326. }
  327. protected function get_connection_parameters()
  328. {
  329. global $_configuration;
  330. $result = array(
  331. 'dbname' => $_configuration['main_database'],
  332. 'user' => $_configuration['db_user'],
  333. 'password' => $_configuration['db_password'],
  334. 'host' => $_configuration['db_host'],
  335. 'driver' => 'pdo_mysql',
  336. );
  337. return $result;
  338. }
  339. }