is there a facility for generating scaffolding in a Symfony2 app? - php

I've been doing searches on scaffolding in Symfony 2 and keep finding references to "generators" but so far have not been able to get scaffolding up and working.
By "scaffolding" I'm referring to a way to point your tool at a database and have it generate views/forms to perform CRUD operations.
This can be useful for quickly prototyping something, and/or build a rough admin tool for some of your database tables.
It can also provide a starting point for some form you are building.
Is this possible in Symfony2?

Crud operations are provided by the SensioGeneratorBundle which is included in the symfony standard distribution.
You can use the following command to generate form, templates & controller for existing entitites. It is interactive and can also update your routing automatically.
app/console generate:doctrine:crud
entity classes themselfes can be created with another command - interactive aswell.
app/console generate:doctrine:entity
Generating entities from database is done with:
app/console doctrine:mapping:convert xml ./src/Acme/BlogBundle/Resources/config/doctrine/metadata/orm --from-database --force
which will create xml mapping files. Afterwards you can generate entities as follows:
app/console doctrine:mapping:import AcmeBlogBundle annotation
app/console doctrine:generate:entities AcmeBlogBundle
This would generate the entities with annotations. yml and xml are supported aswell!

You can generate entities from an existing database like this
Then you can generate CRUD forms for those entities like this
There is no native way to create scaffolding directly from the DB. You have to go through this two step process.

Related

How to use an existing Database with Symfony 6 and Doctrine

I want to use an existing MariaDB with a new Symfony 6 project and Doctrine.
Unfortunately, I don't understand how to create the entities and repository from an existing database. The database is very complex and has a lot of relations. Is there an importer for this?
The following command seems to be deprecated. It works, but only creates the entities (but probably not completely correct).
symfony console doctrine:mapping:import "App\Entity" annotation --path=src/Entity
Thanks for your help.
You can use this kind of command :
symfony console doctrine:mapping:import "App\Entity" annotation --path=src/Entity
But be aware that this solution is deprecated.
What Symfony's team recommend is to create you entities yourself using the maker bundle.
Keep in mind that the maker only generate PHP classes, so you shall be able to reproduce your entities this way.
It's gonna take you some time, but it's the best way to do actually.
Just be careful with the naming of your relations (especially with the many to many pivot tables) wich may not be as generic as what the maker bundle generate

Create a model from existing table (Symfony3)

I have an existing table with 5 columns, and I want to generate an Entity without that generates a new migration with new table!
Thanks!
So, as I mentioned in comment - you just need to follow steps in Symfony documentation.
This way, for Symfony 3 you need follow this:
php bin/console doctrine:mapping:import --force AppBundle xml
This command line tool asks Doctrine to introspect the database and generate the XML metadata files under the src/AppBundle/Resources/config/doctrine folder of your bundle.
Once the metadata files are generated, you can ask Doctrine to build related entity classes by executing the following command.
php bin/console doctrine:mapping:convert annotation ./src
It will generate Entity classes, and after that you must to remove the XML files, generated by first command.

How to remove an entity?

This is my first time using symfony framework. I am trying to learn Doctrine. I have created and entity class. I have created an entity called Product. But since there was something wrong I deleted the Entity folder and now I am trying to create one again. But the framework is not allowing me to do so. It saying that an entity class already exists. How do I remove my previous entity class?
You must to clear doctrine cache:
php app/console doctrine:cache:clear-metadata
php app/console doctrine:cache:clear-query
php app/console doctrine:cache:clear-result
Chen creating your Product entity, what type of mapping do you pick ? XML, YAML, Annotation ?
If you pick XML or YAML, then you got an external file that define your mapping, maybe it's this file that is blocking you.
Take a look to src/Path/To/Your/Bundle/Resources/config/doctrine.

Symfony2: How to generate Entities from MULTIPLE Existing Databases in SAME Bundle?

My goal is to get access to multiple databases in One Project Bundle.
I read through the symfony2 docs and managed to do the followings:
configure multiple connections for different Bundles
generate Entities from ONE Existing Database using:
php app/console doctrine:mapping:import AcmeBlogBundle annotation
php app/console doctrine:generate:entities AcmeBlogBundle
But I cannot find ways to generate Entities from MULTIPLE Existing Databases in SAME Bundle so that I can access multiple databases in One Bundle. Any Ideas?
P.S. I am not familiar with Doctrine. So actually if there are ways to do Symfony2 without Doctrine, I would also appreciate.
UPDATE #1:
Cerad's answer comes quite close. Yet one problem is not yet solved. As I have some same table names in different databases, it's better to organised them into separte folders inside Entity Folder. I have checked similar posts like this and that. But the solutions are not working for me. Their solution simply puts all entities directly into Entity Folder, ignoring the specified dir option in config.yml. Are there workarounds for this problem?
The first step it to configure multiple entity managers (not connections), one for each database. You then use the --em option on the doctrine commands to specify which entity manager to use.
php app/console doctrine:mapping:import "AcmeBlogBundle" annotation --em=name1
php app/console doctrine:mapping:import "AcmeBlogBundle" annotation --em=name2
Be aware that you not going to be able to directly query (join) across multiple database with doctrine. At least not very easily. As long as you plan on limiting your queries to one database at a time then you will be fine.
This is actually a somewhat advanced topic. You might want to spend some time with the doctrine documentation. Might also be easier to get started with one database and then split later.

Is it possible to generate model classes using Doctrine 2 directly from the database?

I am in the process of upgrading from Doctrine 1.1.4 to Doctrine 2.0.6 in my Zend application. I have installed the Doctrine 2 command line tool.
In Doctrine 1.1.4, I generated the model classes directly from the database (using Doctrine::generateModelsFromDb()), is this possible in Doctrine 2, or do I have to go through the 'mapping' process i.e. by providing Docblock Annotations, XML or YAML structures of the tables.
The reason I ask this is because there is a 'setAutoGenerateProxyClass' option in Doctrine 2, I got the impression that this means it will generate the proxy classes from scratch.
Appreciate the help.
Autogenerate proxyclasses means basically that Doctrine 2 will automatically generate "proxy classes" for your entities, instead of only generating them manually using generate-proxies. Proxies are used when you have relations in your entities and they need to be lazy-loaded.
To generate mapping information from the database, you can use convert-mapping:
php doctrine orm:convert-mapping --from-database yml /path/to/mapping-path-converted-to-yml
Bear in mind that this is only recommended to be used as a starting point. The database driver is not able to correctly generate mappings for all possible combinations of options, so you probably should just run this once and then write mappings yourself.
See Doctrine 2 manual, "Reverse Engineering the database"
You can use the "annotation" as driver, if you want to get the generated entities:
php doctrine orm:convert-mapping --from-database annotation generatedModels

Categories