MySQL Table structure not updated in Doctrine ORM Entity - php

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

Related

how to connect to another database in symfony other than the configured one

I want to connect to another database and/or create a new one in symfony. How can I do that and at the same time get access to the tables of it?
I looked for how to do this inside the app
php bin/console doctrine:query:sql 'SELECT * FROM product'
but couldn't find any answer.
You can't create a database directly from Symfony, but you can use Migrations to create tables.
If you want to you can even generate the Migrations using the bin/console and doctrine:migrations:diff to generate migrations based off your Entities.
For the multiple databases you can use Service Containers to configure the Doctrine ORM then in your code you can grab the specific container and instantiate doctrine from there.
Also if you are interested in doing selects I would take a look at DQL as this allows solutions as follows:
// This assumes an entity called 'entity' exists.
$query = $entityManager->createQueryBuilder('entity');
return $query->getResult();
Look at this, should help: https://symfony.com/doc/current/doctrine/multiple_entity_managers.html
I short, you can define different entity managers that deals with different database connections
You need to configure multiple entity managers, check How to Work with multiple Entity Managers and Connections.
Then you can use doctrine commands as below:
php bin/console doctrine:database:create --connection=<your_entity_manager_name>
php bin/console doctrine:query:sql --connection=<your_entity_manager_name>

How to update Symfony Entities from an existing database?

I found in Symfony docs that you can Generate Entities from an Existing Database so are there any ways to update entities from existing database I added using MySQL?
For example, I added new column to my FOSUSERBUNDLE database called "batman"
using this command on MySQL:
ALTER TABLE cs_symfony_members
ADD batman int(11) NOT NULL;
how to update my entities i generated using the docs guide above ?
If you need to update database schema to Entity, you can use two command:
(1) Create entity mapping from database:
bin/console doctrine:mapping:import BundleName --filter=EntityName
(2) Update your entity to match this mapping:
bin/console doctrine:generate:entities BundleName:EntityName
Example, I have a User table in database, a had changed structure of this table. And now, I want to update these changes to my User entity in AppBundle. I need to run following command:
bin/console doctrine:mapping:import AppBundle --filter=User
bin/console doctrine:generate:entities AppBundle:User
I hope this can help you!
For anybody who is having trouble finding this command after forgetting, like me, here it is:
When i update my Entities from my already existing sql database, I juste use this command:
php bin/console doctrine:mapping:import --force AppBundle annotation --filter="Entity name".
I'm using symfony 3.4 but I'm gussing this also works for later versions.

Doctrine/Symfony entity generator and generating entity from one table

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.

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

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.

Categories