我是使用Symfony的新手,今天我遇到了一个问题,我无法更改实体的属性:Doctrine不想将所做的更改插入到我的数据库中...
我寻找诸如here之类的答案,但没有找到解决方法。
当我是新手时,我在一个空白项目中再次尝试:
我创建一个新实体:
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
然后我进行迁移:
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
我运行它:
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
然后我向实体添加了一个新属性:
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
当我尝试创建迁移时,它不起作用:
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.
有人可以帮助我吗?如果我不了解它是如何工作的或其他什么...
谢谢!
答案 0 :(得分:0)
我有同样的问题。清除缓存对我有用。
symfony console cache:clear