dongtang1909
2017-09-01 13:00
浏览 66
已采纳

如何在Symfony 3 / Doctrine中重命名表?

I want to change a table from order to shop_order. First I alter the table name in phpmyadmin.

Furthermore I would have guessed, that changing the annotations would be sufficient:

/**
 * Order
 *
 * @ORM\Table(name="shop_order")
 * @ORM\Entity
 */
class Order { ...

But when running

php bin/console doctrine:schema:update --dump-sql

It tries to create the table again:

CREATE TABLE `order` (id INT AUTO_INCREMENT NOT NULL ...

Is there any other file I need to change?

图片转代码服务由CSDN问答提供 功能建议

我想将表格从 order 更改为 shop_order 。 首先,我改变了phpmyadmin中的表名。

此外我猜测,更改注释就足够了:

  / *  * 
 *订单
 * 
 * @ORM \ Table(name =“shop_order”)
 * @ORM \ Entity 
 * / 
class订单{... 
    
 
 

但是在运行时

  php bin / console doctrine:schema:update --dump-sql 
   
  
 

它尝试再次创建表:

  CREATE TABLE`order`(id INT AUTO_INCREMENT NOT NULL ... 
   
 
 

我还需要更改其他文件吗?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • duandi5328 2017-09-01 13:06
    已采纳

    Clear the cache and try to alter the table name back with phpMyAdmin again. Once it is order again then use the doctrine command:

    php bin/console doctrine:schema:update --dump-sql
    

    If you want Doctrine to execute the changes then add --force as well.

    Also check your Doctrine configuration, it could be that is caching the metadata somewhere else (e.g. Memcache). You might need to clear that as well.

    Doctrine caches on three levels: query cache, metadata cache and result cache. You may want to take a look at the metadata one.

    doctrine:
        orm:
            auto_mapping: true
            # each caching driver type defines its own config options
            metadata_cache_driver: apc
            result_cache_driver:
                type: memcache
                host: localhost
                port: 11211
                instance_class: Memcache
            # the 'service' type requires to define the 'id' option too
            query_cache_driver:
                type: service
                id: my_doctrine_common_cache_service
    

    So, if for example yours is set to Memcache then you'll have to reset Memcache as well.

    Take a look at the docs here.

    已采纳该答案
    打赏 评论
  • draxq02664 2017-09-01 13:01

    try to use force, like this:

    php bin/console doctrine:schema:update --force --dump-sql
    

    And delete the cache please


    If It doesn't work I advise you to use migration because seems tha doctrine doesn't recognize the change of the table name:

    • Generate a new migration.
    • Erase contents of up() and down() methods and replace them with custom SQL (ALTER TABLE ... RENAME TO ...).

    Migration docs

    打赏 评论
  • douci7521 2017-09-01 13:23

    Just change the name with DB administration tool, change the name in the entity accordingly, clear cache and you are good to go - doctrine:schema:update --dump-sql will then export no change.

    打赏 评论

相关推荐 更多相似问题