1
0
mirror of synced 2024-12-15 15:46:02 +03:00

Updating documentation for converting mapping information and reverse engineering databases to entities.

This commit is contained in:
Jonathan H. Wage 2010-05-18 11:49:44 -04:00
parent a59b62e3ba
commit 18b9c6cd69

View File

@ -178,71 +178,63 @@ Before using the orm:schema-tool commands, remember to configure your cli-config
++ Convert Mapping Information ++ Convert Mapping Information
Doctrine comes with some special tools for working with the various supported To convert some mapping information between the various supported formats you can
formats for specifying mapping information. use the `ClassMetadataExporter` to get exporter instances for the different formats:
You have the ability to convert from a few different sources.
* An existing database
* A directory of YAML schema files
* A directory of XML schema files
* A directory of PHP scripts which populate `ClassMetadataInfo` instances
* A directory of PHP classes defining Doctrine entities with annotations
To convert a mapping source you can do everything you need with the `ClassMetadataExporter`.
[php] [php]
$cme = new \Doctrine\ORM\Tools\Export\ClassMetadataExporter(); $cme = new \Doctrine\ORM\Tools\Export\ClassMetadataExporter();
Once you have an instance you can start adding mapping sources to convert. Once you have a instance you can use it to get an exporter. For example, the yml
exporter:
[php] [php]
$cme->addMappingSource('/path/to/yml', 'yml');
$cme->addMappingSource('/path/to/xml', 'xml');
$cme->addMappingSource('/path/to/php', 'php');
$cme->addMappingSource('/path/to/annotations', 'annotation');
Now to convert the added mapping sources you can do so by using the exporter drivers.
[php]
$metadatas = $cme->getMetadatasForMappingSources();
$exporter = $cme->getExporter('yml', '/path/to/export/yml'); $exporter = $cme->getExporter('yml', '/path/to/export/yml');
$exporter->setMetadatas($metadatas);
Now you can export some `ClassMetadata` instances:
$classes = array(
$em->getClassMetadata('Entities\User'),
$em->getClassMetadata('Entities\Profile')
);
$exporter->setMetadata($classes);
$exporter->export(); $exporter->export();
This functionality functionality is also available from the command line to for This functionality is also available from the command line to convert your
example convert some YAML mapping files to XML. loaded mapping information to another format. The `orm:convert-mapping` command
accepts two arguments, the type to convert to and the path to generate it:
$ php doctrine orm:convert-mapping /path/to/mapping-path xml /path/to/mapping-path-converted-to-xml $ php doctrine orm:convert-mapping xml /path/to/mapping-path-converted-to-xml
It is even possible to define more than one path as source:
$ php doctrine orm:convert-mapping --from /path/to/mapping-path1 --from /path/to/mapping-path2 /path/to/mapping-path3 xml /path/to/mapping-path-converted-to-xml
++ Reverse Engineering ++ Reverse Engineering
You can use the same `ClassMetadataExporter` to reverse engineer a database and You can use the `DatabaseDriver` to reverse engineer a database to an array of
generate YAML, XML, etc. from your existing databases. `ClassMetadataInfo` instances and generate YAML, XML, etc. from them.
First you need to retrieve the metadata instances with the `DatabaseDriver`:
[php] [php]
$sm = $em->getConnection()->getSchemaManager(); $em->getConfiguration()->setMetadataDriverImpl(
new \Doctrine\ORM\Mapping\Driver\DatabaseDriver(
$em->getConnection()->getSchemaManager()
)
);
$cme->addMappingSource($sm, 'database'); $cmf = new DisconnectedClassMetadataFactory($em);
$metadatas = $cme->getMetadatasForMappingSources(); $metadata = $cmf->getAllMetadata();
Now you can get an exporter instance and export the loaded metadata to yml:
$exporter = $cme->getExporter('yml', '/path/to/export/yml'); $exporter = $cme->getExporter('yml', '/path/to/export/yml');
$exporter->setMetadatas($metadatas); $exporter->setMetadata($metadatas);
$exporter->export(); $exporter->export();
From the command line it is very simple to do something like reverse engineer You can also reverse engineer a database using the `orm:convert-mapping` command:
your existing database to set of YAML mapping files.
$ php doctrine orm:convert-mapping database yml /path/to/mapping-path-converted-to-yml $ php doctrine orm:convert-mapping --from-database yml /path/to/mapping-path-converted-to-yml
> **CAUTION** > **CAUTION**
> Reverse Engineering is not always working perfectly depending on special cases. > Reverse Engineering is not always working perfectly depending on special cases.
> It will only detect Many-To-One relations (even if they are One-To-One) and > It will only detect Many-To-One relations (even if they are One-To-One) and
> will try to create entities from Many-To-Many tables. It also has problems > will try to create entities from Many-To-Many tables. It also has problems
> with naming of foreign keys that have multiple column names. Any Reverse Engineered > with naming of foreign keys that have multiple column names. Any Reverse Engineered
> Database-Schema needs considerable manual work to become a useful domain model. > Database-Schema needs considerable manual work to become a useful domain model.