Doctrine 2 Reverse Engineering Usage In Zend Framework

I always use Bisna library to integrate Doctrine 2.X with Zend Framework 1.X. This library is created by Guilherme Blanco an Open Source Evangelist and Software Engineer in Yahoo!.

The important thing in Doctrine 2 ORM is the entity. It is just PHP class, that act as the mapper from table in database. For small project with a few tables, make entity manually is not difficult. But in big project with many tables with complicated relation, make entities manually is not easy. It will be easy if we design the tables with tools like MySQL Workbench, create the tables from it, then make the entities with reverse engineering by use Doctrine Console.

Bisna library has prepare Doctrine Console bin/doctrine.php. Based on Bisna configuration on application/configs/application.ini, the driver used is Annotation (see line 124). To make reverse engineering please use this command

$ php doctrine orm:convert-mapping --from-database annotation /path/to/mapping-path-converted-to-annotation

/path/to/mapping-path-converted-to-annotation must same with mappingDirs in application/configs/application.ini (see line 126)

Doctrine Console will generate some PHP files. On each file contain a class with property as mapper from database. Those class have ready to use as entities for Doctrine 2 ORM. But those class still don't have getter and setter. They can be added automatically with this command

$ php doctrine orm:generate-entities --generate-annotations=1 /path/to/entity

/path/to/entity must same with mappingNamespace in application/configs/application.ini (see line 125)

Doctrine Console usage above are enough to get entities by reverse engineering. And if you want get mapper in another type like xml or yaml, just change the type from the type used in convert-mapping.

$ php doctrine orm:convert-mapping --from-database xml /path/to/mapping-path-converted-to-xml
$ php doctrine orm:convert-mapping --from-database yaml /path/to/mapping-path-converted-to-yaml

And to generate entities from those mapping so, please change the driver used in application/configs/application.ini (see line 124). Change it with

"Doctrine\ORM\Mapping\Driver\XmlDriver"

or

"Doctrine\ORM\Mapping\Driver\YamlDriver"

Then use Doctrine Console again

$ php doctrine orm:generate-entities --generate-annotations=1 /path/to/entity

Comments

Awesome article! I'm trying

Awesome article!

I'm trying to learn the use of Doctrine 2 with the ZF 1.11
But is very dificult to find a good tutorial of how to organize my folders and models.

Could you make a full tutorial of the usage of Doctrine with Zend Framework?

Thank you for the really good article! :)

Awesome! =D Could you,

Awesome! =D

Could you, please, make a full tutorial of a Doctrine 2 and Zend Framework 1.11 integration?

Like, how to integrate, generate the PHP classes form the database and the basic usage of it.
I can't find any complete tutorial.
I don't know how to organize my models in modules using the Doctrine.

I appreciate if you help me! =)

tnks from Brazil!

@Vinicius You can look at

@Vinicius

You can look at https://github.com/guilhermeblanco/ZendFramework1-Doctrine2 to start integrate Zend Framework 1.x with Doctrine 2.
And for generating PHP Class (Entities) from database I have explain the way in this post.

Thanks

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options