Skip to content

Instantly share code, notes, and snippets.

@sglessard
Created January 25, 2013 15:46
Show Gist options
  • Select an option

  • Save sglessard/4635399 to your computer and use it in GitHub Desktop.

Select an option

Save sglessard/4635399 to your computer and use it in GitHub Desktop.
Use raw SQL queries with doctrine2 (from the symfony2 web directory)
<?php
/*
* Import data from old application
* http://docs.doctrine-project.org/en/2.0.x/reference/configuration.html
*/
require '../vendor/doctrine/common/lib/Doctrine/Common/ClassLoader.php';
$classLoader = new \Doctrine\Common\ClassLoader('Doctrine\Common', '../vendor/doctrine/common/lib');
$classLoader->register();
$classLoader = new \Doctrine\Common\ClassLoader('Doctrine\DBAL', '../vendor/doctrine/dbal/lib');
$classLoader->register();
$classLoader = new \Doctrine\Common\ClassLoader('Doctrine\ORM', '../vendor/doctrine/orm/lib');
$classLoader->register();
use Doctrine\ORM\EntityManager,
Doctrine\ORM\Configuration;
$cache = new \Doctrine\Common\Cache\ArrayCache; // I don't have APC installed
$config = new Configuration;
$config->setMetadataCacheImpl($cache);
$driverImpl = $config->newDefaultAnnotationDriver('../src/SGL/FLTS');
$config->setMetadataDriverImpl($driverImpl);
$config->setQueryCacheImpl($cache);
$config->setProxyDir('../app/cache/import-data/proxies');
$config->setProxyNamespace('SGL\FLTS\Proxies');
$config->setAutoGenerateProxyClasses(false);
$connectionOptions = array(
'driver' => 'pdo_mysql',
'path' => null,
'host' => 'localhost',
'port' => null,
'dbname' => 'database',
'user' => 'dbuser',
'password' => 'dbpassword'
);
$em = EntityManager::create($connectionOptions, $config);
$query = $em->getConnection()->prepare('SELECT * FROM client');
$query->execute();
$clients = $query->fetchAll();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment