Zend 1.11 and Doctrine 2 Auto generate everything

2020-05-24 20:27发布

问题:

I am new to ORM and I am really keen to learn it. I successfully managed to install all classes and configurations for Doctrine 2.1 with Zend 1.11.x by following this tutorial.

http://www.zendcasts.com/unit-testing-doctrine-2-entities/2011/02/ Which uses Bisna plugin and doctrine scripts.

Now my problem is he is clearly explaining how to create entities and tables through doctrine classes but do not explain how to auto generate the proxies and repo classes from already existing database which helps me to select, insert and update. I always create my databases using MySQL Workbench.

I also followed the below tutorial as well

http://www.zend.com/en/webinar/Framework/70170000000bSrG-webinar-zf-v-1-doctrine-v-2-20101214.flv

My database is so complex with relationship flowing across every possible way. If I follow the steps which is explained in these tutorials I will never complete my project. Can any one please explain how to start using Doctrine after configuration. Considering I already have a database and my Model folders are empty. I have my folder sructure as below.

    C:/zf/library/Doctrine
    C:/zf/library/Symfony
    C:/zf/library/ZC -- (my model which should contain the proxies and repo of Doctrine. At the moment it contains nothing.)
    C:/zf/library/Zend
C:/zf/scripts/doctrine.php

Please help me!

I posted this same post yesterday and no one replied to my post. Please let me know if you need anymore information from me.

Thank you,

Karthik

回答1:

According to Doctrine you should create your entities first yourself and then create your database schema from these entities.

But because you already have a database you probably don't want that. It is possible to convert your database to Doctrine2 entities in PHP, XML or Yaml.

You should take a closer look at the commandline tools Doctrine offers with the Bisna glue because there you can generate a lot of stuff.

To generate your entities FROM your database consider the following command:

php doctrine.php orm:convert-mapping --from-database php ../library/Application/Entity

You can also define the namespace and a base class which your entities have to extends with: --namespace=namespace and --extends=class.

Doctrine2 warns you to convert your database to entities because not everything could be auto detected or supported. For instance ENUM datatypes are not supported by default in Doctrine2 so converting your database will raise an error.

It's a good idea to check all your entities especially associations before you use them. Hope it helps you.



回答2:

If I understand your question correctly, you have your entities already configured and need to auto-generate your proxy and repository classes.

Both can be created using the following Doctrine CLI commands from your application's root directory:

php scripts/doctrine.php orm:generate-proxies

php scripts/doctrine.php orm:generate-repositories library/

If you're looking for a way to auto-generate your entity classes, unfortunately I don't think a solution is available for this yet.

A support rep at ORM Designer said they are "working on" this feature and that it is "very demanded." Here's hoping it will be included in ORM Designer 2.0 since there is generally a lot of repetitive work involved in coding/mapping entity classes that could likely be automated.



回答3:

You can use the orm:generate-entities command if you provide mapping information in either XML or YAML format.

See http://www.doctrine-project.org/docs/orm/2.1/en/reference/tools.html#entity-generation

For development, set proxy generation to be automatic in your config, otherwise, use the orm:generate-proxies command.

Unless you need to customise your repositories, generic ones are created in the entity manager when requested. To specify custom repositories, simply use the repository-class entity mapping attribute.

See http://www.doctrine-project.org/docs/orm/2.1/en/reference/xml-mapping.html#defining-an-entity for an example