Doctrine/Symfony entity generator and generating entity from one table - php

I have already a few entities, but now a new table appeared in a database, and I'd like to generate an entity on only this one table.
I already saw this, but I have further questions.
I already have a User entity (and a db table). Now, the new table is called "Report" (no entity for it right now, I want to create it) and it has a foreign key to User. There are also a few more foreign keys.
If I do what is suggested in the above answer, that is:
$ php app/console doctrine:mapping:import --force AppBundle xml --filter="Report"
$ php app/console doctrine:mapping:convert annotation ./src/AppBundle/Entity --from-database --filter="Report"
$ php app/console doctrine:generate:entities AppBundle:Report --no-backup
Will Doctrine generator try to modify my User entity? Or will just create a Report entity?
Btw. I understand(?), this it will not, because this is ManyToOne relation, but let's assume for a moment that this is ManyToMany for a moment.
I know I could simply try it, but the last time I executed the doctrine:mapping:import --force command I ended up with a crashing app and I spent many hours to solve this problem, until someone on Stackoverflow told me to remove the src\AppBundle/Resources/config/doctrine/ directory, which helped.
I didn't think about making a backup before running this command.
So, I'm a bit afraid of it... Now I created a backup, but I'm not sure if this will help in case of troubles.

It depends on the /Resources/config/doctrine directory. If you empty it out and do the commands you described, then only the Report entity will be modified. If you have all of the previous mappings in that directory it should modify the User entity as well.
The commands will automatically create backups of your entities for you, so you will have a new User.php and a backup in User.php~, which you should be careful of if you are using version control and like to git add src/ without thinking too hard about it. You don't want to add those files to source control.
Either way, backups are created, and you should be using version control on top of that, so you should be fine.

Related

MySQL Table structure not updated in Doctrine ORM Entity

Its symfony 2 - doctrine issue.
I was trying to add some more fields to one of my tables in MySQL. After modification, i had run the command doctrine mapping import and doctrine generate entities commands, but the Entity_name.php file under Bundle/Entity/ folder not getting updated. Can anyone help me ?
Thanks and regards,
Tismon Varghese
Your message is brief, but my interpretation is that you are trying to ADD more fields to an existing table, by creating a NEW entity. This doesn't sound like the correct workflow. The entity ought to already exist, and to add fields to the table, will require adding new properties, correctly annotated, to the existing Entity.
What commands are you running, and what command line output are you seeing? The doctrine based SF2 commands offer reasonable information upon failure.
Try to run this command:
$ php app/console doctrine:schema:update --force

How to remove an entity class in a Symfony2 project

When we initially designed our project, we had a couple of entities that to date are unused (and we don't plan to implement them in the near future). Ergo I want to remove them from my project. I would proceed like this (all steps manually performed):
Remove all relations from my currently used entities.
Delete the doctrime ORM file src/Resources/config/doctrine
Delete the class PHP file from src/Entity
Remove the table from the database
What I would like to know: Are there any routines (e.g. console commands) that may support this procedure? For example, if I run
php app/console doctrine:schema:update --dump-sql
after having removed all relations and deleted the files, that I get the SQL statement that removes the according table(s)?
Once you have removed the entities from your code, you can use the following console command to drop the tables:
php bin/console doctrine:schema:update --complete --dump-sql
Note the use of the --complete option.
Here are the relevant parts from the doctrine:schema:update help text:
Options:
--complete If defined, all assets of the database which are not relevant to the current metadata will be dropped.
[...]
Help:
[...]
Finally, be aware that if the --complete option is passed, this task will drop all database assets (e.g. tables, etc) that are not described by the current metadata. In other words, without this option, this task leaves untouched any "extra" tables that exist in the database, but which aren't described by any metadata.
Hint: If you have a database with tables that should not be managed by the ORM, you can use a DBAL functionality to filter the tables and sequences down on a global level:
$config->setFilterSchemaAssetsExpression($regexp);
to remove the table in symfony 3 you can just run a migration and the table not in use will be dropped from the DB:
php bin/console doctrine:migrations:diff
php bin/console doctrine:migrations:migrate
Your steps to delete an entity are OK.
You can't remove a table from Doctrine, as Doctrine doesn't know about it. Have a look at this question:
Deleting table using Doctrine2 and Symfony2

Is there a "generate-migrations-db" equivalent in Symfony 2 / Doctrine 2?

Here is the legacy documentation explaining what "generate-migrations-db" does:
http://symfony.com/legacy/doc/doctrine/1_2/en/07-Migrations
It says
Generate migration classes from existing database connections
(doctrine-generate-migrations-db, doctrine-gen-migrations-from-db)
Also:
Generating Migrations
Doctrine offers the ability to generate sets of
migration classes for existing databases or existing models as well as
generating blank migration classes for you to fill in with the code to
make your schema changes.
From Database
If you have an existing database you can build a set of migration
classes that will re-create your database by running the following
command.
$ ./symfony doctrine:generate-migrations-db
In other words: it takes the schema from the database and generates a migration that performs that schema creation. No entities, no classes, no mappings are used in this process. It just takes a DB and builds a migration class.
We do not have generate-migrations-db anymore. Do we have something that performs that task? I couldn't find. If it was replaced by some other command, please let me know. If it was just removed, please let know.
I'm not aware of a command in Doctrine or the Migrations Bundle that creates migration files for an existing database.
So here's how I did it instead:
Install DoctrineMigrationsBundle
Create a new blank database
Update your config or parameters to point to this blank database rather than to your "real" one
Run php app/console doctrine:migrations:diff. This will create a migrations file that creates your database tables etc from scratch
Change back your config/parameters
Hope this is helpful.
Take a look at the DoctrineMigrationsBundle, which can generate migration classes with sql statements for migration.

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.

Doctrine (on Ubuntu): What command generate models from database without delete existing models?(I add new tables soo i want update models)

Doctrine (on Ubuntu): What command generate models from database without delete existing models?(I add new tables soo i want update models)
Thanks
If you're using Doctrine 1, you'll need to make use of Migrations. Migrations can either be written manually, or auto-generated one of a few different commands:
./doctrine generate-migration
./doctrine generate-migrations-db
./doctrine generate-migrations-models
Migrations can be a fairly complex topic, so I would read up on the documentation. You may also want to check out this slide show.
If you're using Doctrine 2, things get a little simpler. It doesn't have a Migrations class (yet), but there an easy-to-use command to non-destructively update your db schema:
./doctrine orm:schema-tool:update
This has worked well for me, but can sometimes fail due to complex foreign key constraints.

Categories