delete
delete

Reputation: 19148

Auto generate Doctrine-Entity from existing Table

I'm trying to auto generate entities from a specific table of my database. I don't want to send doctrine over the whole database, because there are tables in it which are not compatible with doctrine (like tables from the legacy database having no primary keys [yes...]).

Unfortunately, all the blog posts and SO-Anwers doesn't help, as parameter "--filter" isn't working as supposed.

What i've done:

i tried the first step from this SO-Answer and also read this blog post.

php app/console doctrine:mapping:convert metadata_format \
    ./src/App/UserBundle/Resources/config/doctrine \
    --from-database \
    --filter="user"

I understood the filter-argument as a solution to filter only for a specific table. But this filter is not being applied. The process gets canceled by an exception "Table XYZ has no primary key" (what is true here and is not part of the question).

I guess i could create a complete new database without the problematic tables for creating the annotations and entities. But i'm wondering about that what is written about this process and why it does not work.

UPDATE

I also tried out the suggestions from the first answer:

php app/console doctrine:mapping:import --force AcmeBlogBundle yml

AcmeBlogBundle is placeholder for my real bundle. Then the import task stops with:

[Symfony\Component\Debug\Exception\ContextErrorException]
Warning: class_parents(): Class Action does not exist and could not be loaded

The very first table is called "action". When i drop this table, it stops with a simular exception at the next table.

UPDATE 2

The exception mentioned some lines above where caused due to a previous used ORM-Implementation having Classes like "ActionEntity.php" within a Directory called "Entity" for each table. Thats why the exception happened i think. After i dropped this directory, the exception disappeared (but it isn't a good solution in my case).

What I did now: i dropped ALL foreign keys (they made troubles), i setup a completely new symfony project, i run then all these comments from the first answer here and then i got pre-generated entity classes, but without any foreign keys implementation. Those things i have to add now, including several changes to the database scheme because doctrine 2 has some restrictions here like not supporting primary keys as foreign keys and something like that. But now i can pick those entity-classes and adjust missing connections to other entities.

All in all: this whole process is not really satisfying ...

Upvotes: 2

Views: 7231

Answers (2)

lucsan
lucsan

Reputation: 850

I'm using the commands programatically, This one reads the database indicated in the config.yml file

# Doctrine Configuration
doctrine:
    dbal:
        default_connection: dbalYmlConfig
        connections:

The commands are:-

       'command' => 'doctrine:mapping:convert',
       'to-type' => 'yml',
       'dest-path' => $rawOrmTempSavePath,
       '--from-database' => true,
       '--force' => true,

This creates x.orm.yml file mapping for each database table schema.

This creates the Entity files from the x.orm.yml's which are in Resources/config/doctrine

       'command' => 'doctrine:generate:entities',
       'name' => 'MyBundle',

Side note: I put the x.orm.yml files in a temp directory then copy them into the config/doctrine folder adding the bundle name to the x.orm.yml file header, but you could dump the files straight into config/doctrine.

Anyhoo this method works for me.

Upvotes: 0

Max Lipsky
Max Lipsky

Reputation: 1842

From documentation:

As the Doctrine tools documentation says, reverse engineering is a one-time process to get started on a project. Doctrine is able to convert approximately 70-80% of the necessary mapping information based on fields, indexes and foreign key constraints. Doctrine can't discover inverse associations, inheritance types, entities with foreign keys as primary keys or semantical operations on associations such as cascade or lifecycle events. Some additional work on the generated entities will be necessary afterwards to design each to fit your domain model specificities.

Right steps to import entities from database:

  1. php bin/console doctrine:mapping:import --force AcmeBlogBundle xml
  2. php bin/console doctrine:mapping:convert annotation ./src
  3. php bin/console doctrine:generate:entities AcmeBlogBundle

You can use xml or yml, however it worked only with xml for my case.

Upvotes: 3

Related Questions