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"

Create a Title / Subtitle Pair in from a Controllers Name</a></h2> </header><!-- .entry-header --> <div class="entry-content"> <p>If I have a website called Blog with two main sub pages; about me and bio, I like to have the HTML title to always be My Site: Current page. Since all of my controllers have descriptive names, I can just put the following in my layout:</p> <p><code lang="php"><br /> <title>Blog: <?php echo ucwords(str_replace("-", " ", Zend_Controller_Front::getInstance()->getRequest()->getControllerName()));?>

This would give me the following when users navigate to my site:

Blog (mysite.com)
Blog: About Me (mysite.com/about-me)
Blog: Bio (mysite.com/bio)