Search code examples
symfonydoctrinesymfony4

How to define entire database schema in Symfony 4?


Is there a way to define the complete database schema in one go in Symfony 4?

I understand that individual entities/objects can be created using the make:entity and make:migration commands but I'm wondering if I could just define the entire schema in one sitting and then use it to build the associated entities and database.

I recall that in earlier versions of Symfony it was possible to define the entire schema in a YAML file and then just issue a build command.


Solution

  • Yes, you can create complete database schema mappings using any of supported mapping formats (e.g. YAML or XML) and declare mappings location in Doctrine configuration. After that you will be able to use any Doctrine console tools to generate and update schema. You can also use tools for reverse engineering mappings from already available database and to convert mappings between formats

    Please notice that Doctrine commands names in Symfony application are different from ones that natively provided by Doctrine. You need to use:

    • doctrine:schema:validate for schema validation
    • doctrine:schema:create for initial schema generation with subsequent calls of doctrine:schema:update with either --dump-sql or --force depending on your needs
    • doctrine:mapping:convert to reverse engineer available database (with use of --from-database option) or convert between mapping types in a case if you want to.