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(
array('path/to/your/xml/files'));
/* @var $em DoctrineORMEntityManager */
$em = $application->getServiceManager()->get('doctrine.entitymanager.orm_default');
$em->getConfiguration()->setMetadataDriverImpl($driverImpl);
// end of new code

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

4) Create the php entity files:

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

Leave a Reply

Your email address will not be published. Required fields are marked *