Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace Doctrine;
- use Doctrine\ORM\Tools\Setup;
- use Doctrine\ORM\EntityManager;
- class Service {
- /* -- Attributs -- */
- /**
- * @var manager \Doctrine\ORM\EntityManager
- */
- private $manager;
- /* -- Constructeur -- */
- /**
- * __construct
- * @param dsn String
- */
- public function __construct($driver, $host, $database, $user, $passwd) {
- \Wn\Bin\ClassLoader::setIncludePath(LIB_PATH .'doctrine/Doctrine');
- \Wn\Bin\ClassLoader::setIncludePath(LIB_PATH .'doctrine/Symfony');
- require_once 'Doctrine/Common/ClassLoader.php';
- $loader = new \Doctrine\Common\ClassLoader("Doctrine");
- $loader->register();
- $dbParams = array(
- 'driver' => $driver,
- 'host' => $host,
- 'dbname' => $database,
- 'user' => $user,
- 'password' => $passwd
- );
- $path = 'entities';
- $config = Setup::createAnnotationMetadataConfiguration( Array($path), true);
- $this->manager = EntityManager::create($dbParams, $config);
- /*
- $classLoader = new \Doctrine\Common\ClassLoader('Doctrine');
- $classLoader->register();
- $classLoader = new \Doctrine\Common\ClassLoader('Symfony', 'Doctrine');
- $classLoader->register();
- $configFile = getcwd() . DIRECTORY_SEPARATOR . 'cli-config.php';
- $helperSet = null;
- if (file_exists($configFile)) {
- if ( ! is_readable($configFile)) {
- trigger_error(
- 'Configuration file [' . $configFile . '] does not have read permission.', E_ERROR
- );
- }
- require $configFile;
- foreach ($GLOBALS as $helperSetCandidate) {
- if ($helperSetCandidate instanceof \Symfony\Component\Console\Helper\HelperSet) {
- $helperSet = $helperSetCandidate;
- break;
- }
- }
- }
- $helperSet = ($helperSet) ?: new \Symfony\Component\Console\Helper\HelperSet();
- \Doctrine\ORM\Tools\Console\ConsoleRunner::run($helperSet);
- */
- echo __METHOD__ .'<hr />';
- }
- public function getManager() {
- if ($this->manager == null) {
- try {
- Setup::registerAutoloadPEAR();
- // Create a simple "default" Doctrine ORM configuration for XML Mapping
- $isDevMode = true;
- $config = Setup::createXMLMetadataConfiguration(array(__DIR__."/config/xml"), $isDevMode);
- // or if you prefer yaml or annotations
- //$config = Setup::createAnnotationMetadataConfiguration(array(__DIR__."/entities"), $isDevMode);
- //$config = Setup::createYAMLMetadataConfiguration(array(__DIR__."/config/yaml"), $isDevMode);
- // database configuration parameters
- $conn = array(
- 'driver' => 'pdo_sqlite',
- 'path' => __DIR__ . '/db.sqlite',
- );
- $config = new \Doctrine\DBAL\Configuration();
- $connectionParams = array(
- 'dbname' => 'mydb',
- 'user' => 'user',
- 'password' => 'secret',
- 'host' => 'localhost',
- 'driver' => 'pdo_mysql',
- );
- $conn = DriverManager::getConnection($connectionParams, $config);
- // obtaining the entity manager
- $this->manager = \Doctrine\ORM\EntityManager::create($conn, $config);
- }
- catch(Exception $e) {
- echo $e;
- }
- }
- return $this->manager;
- }
- }
Add Comment
Please, Sign In to add comment