Creating Entities from Existing Database with Doctrine 2 and Zend 2

So I’m upgrading an existing system to use Doctrine, Zend 2 and Dojo and the first step was to setup Doctrine with the existing database. I really didn’t want to have to manually code up all of the Entities so I was looking to see how I could use Doctrines tools to automate this. Here’s how:

(note, I’m using Windows)
1) Create a “tmp” directory in the root of your site

2) From the root of your Zend 2 site, run

php vendordoctrinedoctrine-modulebindoctrine-module orm:convert-mapping —–from-database xml .tmp

3) Once the .dcm.xml files have been generated, you need to tell Doctrine to use the XML driver by updating the vendordoctrinedoctrine-modulebindoctrine-module.php file:

// Add the following three statements above the last two lines
$driverImpl = new DoctrineORMMappingDriverXmlDriver(
/* @var $em DoctrineORMEntityManager */
$em = $application->getServiceManager()->get('doctrine.entitymanager.orm_default');
// end of new code

/* @var $cli SymfonyComponentConsoleApplication */
$cli = $application->getServiceManager()->get('doctrine.cli');

4) Create the php entity files:

php vendordoctrinedoctrine-modulebindoctrine-modules orm:generate-entities ----generate-annotations="true"

Getting Symfony 1.4 to Run on a Windows Server Running PHP >= 5.3

If you’re running a LAMP environment, you might wonder what’s the big deal, it should just work right? No. When PHP upgraded from the 5.2 series to the 5.3 series, it dropped its support for the MSSQL driver (php_mssql.dll) and was not going to continue support. You had to use Microsofts SQLSRV driver ( Well that’s ok, you could use the PDO_DBLIB… nope, PHP quit supporting that on all Windows servers running PHP >= 5.3. ( *fudge*

Ok, so now we have the SQLSRV driver from Microsoft, sweet. No… wait… Symfony 1.4 uses a Doctrine version that has no idea what SQLSRV is! *sigh* Hmm, maybe we can use the PDO_SQLSRV that Microsoft developed? Well, that should work (and actually does) except the older Doctrine version is not aware of pdo_sqlsrv’s existence. So lets make it aware!

Making Symfony 1.4 PDO_SQLSRV aware

Before you being, please make sure you have the following installed:

PDO SQL Drivers:

SQL Server 2012 Client:

First we need to create the PDO_Sqlsrv class. I’ll I did was copy the existing Doctrine_Connection_Mssql class and renamed it to Doctrine_Connection_Sqlsrv. You can do this because there is nothing special about the SQL code, it’s all just handled by the DLL. Doctrine just needs a class to run.

Once you’ve done that, you need to tell Doctrine_Manager about your new connection driver. Go to line 82 and add your new class to the $_connectionDrivers array:

protected $_connectionDrivers = array(
'db2' => 'Doctrine_Connection_Db2',
'mysql' => 'Doctrine_Connection_Mysql',
'mysqli' => 'Doctrine_Connection_Mysql',
'sqlite' => 'Doctrine_Connection_Sqlite',
'pgsql' => 'Doctrine_Connection_Pgsql',
'oci' => 'Doctrine_Connection_Oracle',
'oci8' => 'Doctrine_Connection_Oracle',
'oracle' => 'Doctrine_Connection_Oracle',
'mssql' => 'Doctrine_Connection_Mssql',
'dblib' => 'Doctrine_Connection_Mssql',
'odbc' => 'Doctrine_Connection_Mssql',
'sqlsrv' => 'Doctrine_Connection_Sqlsrv',
'mock' => 'Doctrine_Connection_Mock'

Finally, tell Doctrine to use Sqlsrv (config_databases.yaml)

return array(
'doctrine' => new sfDoctrineDatabase(array (
'dsn' => 'sqlsrv:server=my.db.server;Database=my_db',
'username' => $username,
'password' => $password,
'name' => 'doctrine',

Wala, Symfony 1.4 should now be running on your Windows Server running PHP 5.3 and 5.4.