Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to generate model classes using Doctrine 2 directly from the database?

I am in the process of upgrading from Doctrine 1.1.4 to Doctrine 2.0.6 in my Zend application. I have installed the Doctrine 2 command line tool.

In Doctrine 1.1.4, I generated the model classes directly from the database (using Doctrine::generateModelsFromDb()), is this possible in Doctrine 2, or do I have to go through the 'mapping' process i.e. by providing Docblock Annotations, XML or YAML structures of the tables.

The reason I ask this is because there is a 'setAutoGenerateProxyClass' option in Doctrine 2, I got the impression that this means it will generate the proxy classes from scratch.

Appreciate the help.

like image 898
Mr B Avatar asked Jul 11 '11 16:07

Mr B


2 Answers

Autogenerate proxyclasses means basically that Doctrine 2 will automatically generate "proxy classes" for your entities, instead of only generating them manually using generate-proxies. Proxies are used when you have relations in your entities and they need to be lazy-loaded.

To generate mapping information from the database, you can use convert-mapping:

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

Bear in mind that this is only recommended to be used as a starting point. The database driver is not able to correctly generate mappings for all possible combinations of options, so you probably should just run this once and then write mappings yourself.

See Doctrine 2 manual, "Reverse Engineering the database"

like image 119
Jani Hartikainen Avatar answered Sep 30 '22 00:09

Jani Hartikainen


You can use the "annotation" as driver, if you want to get the generated entities:

php doctrine orm:convert-mapping --from-database annotation generatedModels
like image 22
Kristóf Dombi Avatar answered Sep 29 '22 23:09

Kristóf Dombi