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.
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.
Related
I have to do large refactoring/enhancements on web apps. We decided to use Symfony4.
I want to define 2 (or more) databases: the old one and the new one.
(In the future, I think to have more location, person databases common at several web apps)
In my researches, I use Multiple Entity Managers, create my databases as mentionned, then create my src/Entity/Main and src/Entity/Customer folders.
Then, I want to create my entities, especially new one with php bin/console make:entity but it creates files in Entity folder, not in Entity/Main (default) or Customer and returns
[ERROR] Only annotation mapping is supported by make:entity
Are there any solution to use this make:entity command or should I define all entity files myself?
Nota: I don't put my config/packages/doctrine.yaml, it is the same as in help sample except server_version: '5.6' to be compliant with my MariaDB version
The answer to create Product entity in Main is:
php bin/console make:entity Main\\Product
and to create Customer entity in Customer is:
php bin/console make:entity Customer\\Customer
With Symfony 3 and its console, we can generate entities from an already existing database via the command "php bin/console doctrine:mapping:import" (very usefull !).
From symfony 4, the command "./bin/console doctrine:mapping:import" needs a bundle name but symfony 4 doesn't work with bundle now.
With the new version of symfony, is there a way I didn't see for generate entities from an existing Database (mysql by example) ? Or must I wait a new version of doctrine for have a "doctrine:mapping:import" compatible with Symfony 4 ?
I found a(n) (ugly) solution yet. I deploy a disposable symfony 3, I link the symfony 3 to my database and I generate entities in a bundle. Then I copy generates files to symfony 4.
It's ugly but it works haha
You can use
php bin/console doctrine:mapping:convert --from-database annotation ./src/Entity
which should create the entities based on the database setting. Don’t forget to add the namespaces, and you will still need to add the getters and setters, but the bulk of the properties, including annotations and some of the relationships are already included. (Source)
Please also note, that Doctrine will not support this anymore in the next Doctrine version. As written in the Symfony docs
Moreover, this feature to generate entities from existing databases will be completely removed in the next Doctrine version.
The structure of the database I am using for my project is constantly changed by another team. So I have to import regularly this structure into Symfony. To do so I use the following command:
php app/console doctrine:mapping:import --force EgBundle yml [--filter="Table"]
php app/console doctrine:generate:entities [Company/EgBundle/Entity/Table]
Every time I run those command I loose the declaration of my repositories and I have to had them manually:
repositoryClass: Company\EgBundle\Entity\TableRepository
Is there any way to keep this declaration?
There is no way for Doctrine to know what the repository class shall be when importing a mapping from database. The information can't be obtained from the database schema.
A possible solution for your use-case would be extending\overriding the doctrine:mapping:import command to add a Repository to the mapping information automatically using a naming convention.
I am working with Symfony 2.3 on a new project using an existing database with numerous associations - many-to-many, one-to-many etc. During my initial import last week, I found somewhere in the docs stipulated that a doctrine:mapping:import would generate orm.yml files of my database which it did without a hitch. However, I also see that only ManytoOne relationships are generated in the yml files ... not any other kind of associations.
My statement was:
$ php app/console doctrine:mapping:import –em=buv DBImportTestBundle yml
Also, I did a generate entities to create classes and basic CRUD for each table using:
$ php app/console doctrine:generate:entities DBImportTestBundle
This also worked EXCEPT that I do not see any annotated associations generated in the doc blocks for any of the entity properties.
I'm looking through the docs but do not see any specific information on the exact requirements for associations on imported dbs. It could be I'm not looking in the right place.
I'm trying to determine the most efficient way to maintain my db schema within symfony/doctrine ... My understanding was that I would need to explicitly define certain associations manually but I'm not sure what the exact requirements would be OR if perhaps I'm simply not passing in the correct arguments to create my annotated associations via generate:entities.
Can someone point me to any docs that refer to what I'm talking about or explain the proper approach to defining complex associations within doctrine? Thank you.
$ php app/console doctrine:mapping:convert annotation ./src to generate entity classes with annotation mappings, before running :
$ php app/console doctrine:generate:entities DBImportTestBundle
there is a cookbook for that
I get that it saves into PHPCR through Doctrine ODM.
But I get that it is all saved in app.sqlite, but I don't see how does it works conceptually ?
Why does it need the database at all when everything is saved in app.sqlite?
Why do I need to make this kind of commands php app/console doctrine:phpcr:init:dbal and so on ?
Symfony CMF can work with different model classes. we provide a default implementation that is mapped for Doctrine PHPCR-ODM. PHPCR-ODM in turn is built on the php content repository PHPCR. there are 3 implementations of that, the cmf sandbox by default uses jackalope-doctrine-dbal. jackalope-doctrine-dbal in turn uses doctrine dbal to store content into databases supported by doctrine dbal. jackalope currently handles sqlite, mysql and postgres. jackalope-doctrine-dbal needs some initialization on the first run, which are the commands you have seen. this is the same whether we use the embedded sqlite database driver or mysql or postgres.
You find some background for the choices in choosing a storage layer and information how to set up phpcr-odm in Create a New Project with PHPCR-ODM. Look around at http://symfony.com/doc/master/cmf/index.html to have most of the CMF concepts and implementation explained.