Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to rename a table in 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?

like image 835
ptmr.io Avatar asked Sep 01 '17 13:09

ptmr.io


2 Answers

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.

like image 76
Francesco Casula Avatar answered Oct 02 '22 04:10

Francesco Casula


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

like image 28
Alessandro Minoccheri Avatar answered Oct 02 '22 06:10

Alessandro Minoccheri