Guest User

Untitled

a guest
Sep 24th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.14 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Doctrine;
  4.  
  5. use Doctrine\ORM\Tools\Setup;
  6. use Doctrine\ORM\EntityManager;
  7.  
  8.  
  9. class Service {
  10.  
  11. /* -- Attributs -- */
  12.  
  13. /**
  14. * @var manager \Doctrine\ORM\EntityManager
  15. */
  16. private $manager;
  17.  
  18.  
  19. /* -- Constructeur -- */
  20.  
  21.  
  22. /**
  23. * __construct
  24. * @param dsn String
  25. */
  26. public function __construct($driver, $host, $database, $user, $passwd) {
  27.  
  28. \Wn\Bin\ClassLoader::setIncludePath(LIB_PATH .'doctrine/Doctrine');
  29. \Wn\Bin\ClassLoader::setIncludePath(LIB_PATH .'doctrine/Symfony');
  30.  
  31. require_once 'Doctrine/Common/ClassLoader.php';
  32.  
  33. $loader = new \Doctrine\Common\ClassLoader("Doctrine");
  34. $loader->register();
  35.  
  36. $dbParams = array(
  37. 'driver' => $driver,
  38. 'host' => $host,
  39. 'dbname' => $database,
  40. 'user' => $user,
  41. 'password' => $passwd
  42. );
  43.  
  44. $path = 'entities';
  45. $config = Setup::createAnnotationMetadataConfiguration( Array($path), true);
  46. $this->manager = EntityManager::create($dbParams, $config);
  47.  
  48.  
  49. /*
  50. $classLoader = new \Doctrine\Common\ClassLoader('Doctrine');
  51. $classLoader->register();
  52.  
  53. $classLoader = new \Doctrine\Common\ClassLoader('Symfony', 'Doctrine');
  54. $classLoader->register();
  55.  
  56. $configFile = getcwd() . DIRECTORY_SEPARATOR . 'cli-config.php';
  57.  
  58. $helperSet = null;
  59. if (file_exists($configFile)) {
  60. if ( ! is_readable($configFile)) {
  61. trigger_error(
  62. 'Configuration file [' . $configFile . '] does not have read permission.', E_ERROR
  63. );
  64. }
  65.  
  66. require $configFile;
  67.  
  68. foreach ($GLOBALS as $helperSetCandidate) {
  69. if ($helperSetCandidate instanceof \Symfony\Component\Console\Helper\HelperSet) {
  70. $helperSet = $helperSetCandidate;
  71. break;
  72. }
  73. }
  74. }
  75.  
  76. $helperSet = ($helperSet) ?: new \Symfony\Component\Console\Helper\HelperSet();
  77.  
  78. \Doctrine\ORM\Tools\Console\ConsoleRunner::run($helperSet);
  79. */
  80. echo __METHOD__ .'<hr />';
  81. }
  82.  
  83.  
  84. public function getManager() {
  85. if ($this->manager == null) {
  86. try {
  87.  
  88. Setup::registerAutoloadPEAR();
  89. // Create a simple "default" Doctrine ORM configuration for XML Mapping
  90. $isDevMode = true;
  91. $config = Setup::createXMLMetadataConfiguration(array(__DIR__."/config/xml"), $isDevMode);
  92. // or if you prefer yaml or annotations
  93. //$config = Setup::createAnnotationMetadataConfiguration(array(__DIR__."/entities"), $isDevMode);
  94. //$config = Setup::createYAMLMetadataConfiguration(array(__DIR__."/config/yaml"), $isDevMode);
  95.  
  96. // database configuration parameters
  97. $conn = array(
  98. 'driver' => 'pdo_sqlite',
  99. 'path' => __DIR__ . '/db.sqlite',
  100. );
  101.  
  102. $config = new \Doctrine\DBAL\Configuration();
  103.  
  104. $connectionParams = array(
  105. 'dbname' => 'mydb',
  106. 'user' => 'user',
  107. 'password' => 'secret',
  108. 'host' => 'localhost',
  109. 'driver' => 'pdo_mysql',
  110. );
  111. $conn = DriverManager::getConnection($connectionParams, $config);
  112.  
  113.  
  114.  
  115. // obtaining the entity manager
  116. $this->manager = \Doctrine\ORM\EntityManager::create($conn, $config);
  117. }
  118. catch(Exception $e) {
  119. echo $e;
  120. }
  121. }
  122.  
  123. return $this->manager;
  124. }
  125.  
  126. }
Add Comment
Please, Sign In to add comment