Search code examples
symfonydoctrine-orm

Entities aren’t updated after changes


I'm new to using Symfony, and today I encountered a problem, I can not change the properties of my entities : Doctrine doesn't want to insert the changes into my database...

I look for answers like for example here, but I did not find the solution.

As I am new, I tried again from a blank project:

I create a new entity:

luc@Lucs-MacBook-Pro:~/SymfonyTest|master⚡ 
⇒  symfony console make:entity             

 Class name of the entity to create or update (e.g. TinyChef):
 > Product

 created: src/Entity/Product.php
 created: src/Repository/ProductRepository.php
 
 Entity generated! Now let's add some fields!
 You can always add more fields later manually or by re-running this command.

 New property name (press <return> to stop adding fields):
 > name

 Field type (enter ? to see all types) [string]:
 > 

 Field length [255]:
 > 

 Can this field be null in the database (nullable) (yes/no) [no]:
 > 

 updated: src/Entity/Product.php

 Add another property? Enter the property name (or press <return> to stop adding fields):
 > 


           
  Success! 
           

 Next: When you're ready, create a migration with make:migration

Then I make the migration:

luc@Lucs-MacBook-Pro:~/SymfonyTest|master⚡ 
⇒  symfony console make:migration

           
  Success! 
           

 Next: Review the new migration "src/Migrations/Version20191122111110.php"
 Then: Run the migration with php bin/console doctrine:migrations:migrate
 See https://symfony.com/doc/current/bundles/DoctrineMigrationsBundle/index.html

I run it:

luc@Lucs-MacBook-Pro:~/SymfonyTest|master⚡ 
⇒  symfony console doctrine:migration:migrate 
                                                              
                    Application Migrations                    
                                                              

WARNING! You are about to execute a database migration that could result in schema changes and data loss. Are you sure you wish to continue? (y/n)y
Migrating up to 20191122111110 from 0

  ++ migrating 20191122111110

     -> CREATE TABLE product (id INT AUTO_INCREMENT NOT NULL, name VARCHAR(255) NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB

  ++ migrated (took 36.5ms, used 16M memory)

  ------------------------

  ++ finished in 39ms
  ++ used 16M memory
  ++ 1 migrations executed
  ++ 1 sql queries

And I added a new property to my entity:

luc@Lucs-MacBook-Pro:~/SymfonyTest|master⚡ 
⇒  symfony console make:entity                

 Class name of the entity to create or update (e.g. FierceGnome):
 > Product

 Your entity already exists! So let's add some new fields!

 New property name (press <return> to stop adding fields):
 > description

 Field type (enter ? to see all types) [string]:
 > 

 Field length [255]:
 > 

 Can this field be null in the database (nullable) (yes/no) [no]:
 > 

 updated: src/Entity/Product.php

 Add another property? Enter the property name (or press <return> to stop adding fields):
 > 


           
  Success! 
           

 Next: When you're ready, create a migration with make:migration

When I try to create my migration it doesn't work:

luc@Lucs-MacBook-Pro:~/SymfonyTest|master⚡ 
⇒  symfony console make:migration             

                                                                                                                        
 [WARNING] No database changes were detected.                                                                           
                                                                                                                        

 The database schema and the application mapping information are already in sync.

Can someone help me? If I didn't understand how it works or something else...

Thanks!


Solution

  • I had the same issue. Clearing the cache worked for me.

    symfony console cache:clear