Generating entities from database

15,215

Solution 1

Your case is well documented in symfony website titled "How to generate Entities from an Existing Database"

As the documentation stated:

The first step towards building entity classes from an existing database is to ask Doctrine to introspect the database and generate the corresponding metadata files. Metadata files describe the entity class to generate based on table fields.

Using following command (assuming your bundle's short name is GOutsideGOBundle)

$ php app/console doctrine:mapping:import --force GOutsideGOBundle xml

Then you need to call.

php app/console doctrine:generate:entities GOutsideGOBundle

If you need to generate entity classes with annotation mappings, then you have to execute the following command before doctrine:generate:entities

php app/console doctrine:mapping:convert annotation ./src

Path should be only ./src instead of ./src/GOutside/GOBundle/Resources/config/doctrine

Updated:

If everything is correct in your configuration then getting the error Database does not have any mapping information. is unlikely!! I am not sure about this error. But as per your database table schema, there are some issue which will prevent you from creating mapping information.

  1. Your table contain special type point which doctrine can't handle.
  2. You database table has multiple table without any primary key. Doctrine does not support reverse engineering from tables that don't have a primary key

To solve problem (1) you can add a custom mapping in your doctrine config section. For example to map the point type as string you can write:

doctrine:
    dbal:
        //Other connection parameters
        mapping_types:
            point: string

for the second problem you need to define primary key for those tables, those could be new field or could be composite primary key.

Solution 2

I got this error

Database does not have any mapping information.

in my case it was because I have 2 tables named like these:

  1. mockstore_category
  2. mockstore_product

when I tried with this command:

sudo php app/console doctrine:mapping:import --filter="MockstoreProduct" --force MockizartStoreBundle yml

the error Database does not have any mapping information. appeared. but it didn't, when I used it to generate --filter="MockstoreCategory".

So to generate mapping for mockstore_product table, it should be:

php app/console doctrine:mapping:import --filter="Product" --force MockizartStoreBundle yml

--filter="Product" NOT --filter="MockstoreProduct"

Hope this help.

Solution 3

Had the same problem, and it turned out it was the cache:

console c:c

Solved the problem.

Share:
15,215

Related videos on Youtube

Długi
Author by

Długi

.

Updated on June 04, 2022

Comments

  • Długi
    Długi almost 2 years

    I'm trying to generate entities in symfony2 from an existing empty database (quite large and it would be real pain to create entities from scratch). Sadly, I've encountered rather big problem.

    When I try to call the following command (on windows if it changes anything):

    php app/console doctrine:mapping:convert --force --from-database annotation ./src/GOutside/GOBundle/Resources/config/doctrine
    

    I got following message:

    No Metadata Classes to process.

    Before posting I've:

    • Validated that my configuration is ok (I can connect to database),
    • My bundle is created and path specified above is valid
    • Same thing happens when I try to convert to xml/yml as when I try to generate annotation mapping.

    Symfony version is 2.4.4, php version is 5.5.3.

    Thanks for help in advance :)

    Edit after xiidea reply

    When I try to use

    php app/console doctrine:mapping:import --force GOBundle xml
    

    as suggested by @xiidea, I got following message:

    Database does not have any mapping information.

  • Długi
    Długi almost 10 years
    Seems like I missed that import step then, but sadly - that won't do it, getting Database does not have any mapping information. message if I try to do doctrine:mapping:import.
  • xiidea
    xiidea almost 10 years
    @Długi: As far As i remember Database does not have any mapping information shows when there is no table. Could you check your database configuration, it those are correct or not? specially the database name. If your info are correct, could you share one or two table schema?
  • Długi
    Długi almost 10 years
    it's basic mysql configuration, so a no brainer (no password, localhost, etc.) but here is table export - pastebin.com/02tnzgp6 hope this can help!
  • xiidea
    xiidea almost 10 years
    @Długi: If everything you are telling is as it is, I,m not sure why you are getting such error!!. But as per your table schema it should not work for several reasons: 1. your table contain special type point which doctrine can't handle. 2. you database table has multiple table without any primarykey. Doctrine does not support reverse engineering from tables that don't have a primary key
  • Gábor Nagy
    Gábor Nagy over 8 years
    This answer gave a useful hint to me, thanks. I was trying --filter="calendar" but it only works with uppercase name like --filter="Calendar".
  • Azhar Khattak
    Azhar Khattak over 6 years
    One Up Vote for this answer as it mentions Case issue. Thanks!