Add a column to an existing entity in Symfony

前端 未结 9 1880
误落风尘
误落风尘 2020-12-22 21:27

I\'ve been playing around with Symfony on my web server and I\'ve been creating entity with doctrine for my database. I wanted to add a column to one of these entity... I wa

9条回答
  •  执笔经年
    2020-12-22 21:42

    There is a pitfall on step php app/console doctrine:migrations:diff described above when using doctrine migrations.

    You made changes in entity, all seems valid, but the command on creating migrations php app/console doctrine:migrations:diff says "No changes detected in your mapping information."

    And you can't find where's the old database structure placed, search in files found nothing.

    For me, the key was to clear Redis cache.

    php app/console redis:flushdb
    

    It happens because of config.yml

    snc_redis:
        clients:
            default:
                type: predis
                alias: default
                dsn: redis://localhost
        doctrine:
            query_cache:
                client: default
                entity_manager: default
                namespace: "%kernel.root_dir%"
            metadata_cache:
                client: default
                entity_manager: default
                document_manager: default
                namespace: "%kernel.root_dir%"
            result_cache:
                client: default
                namespace: "%kernel.root_dir%"
                entity_manager: [default, read]  # you may specify multiple entity_managers
    

    After that, migrations:diff reread all of entities (instead of taking outdated ones metadata from cache) and created the right migration.


    So, the full chain of steps for modifying entities is:

    1. Modify your entity class (edit Entity file)
    2. Clear Redis cache of metadata ( php app/console redis:flushdb )
    3. Create (and may be edit) migration ( php app\console doctrine:migrations:diff )
    4. Execute migration ( php app\console doctrine:migrations:migrate )

    Of course, your doctrine metadata cache may be not Redis but something else, like files in app/cache or anything other. Don't forget to think about cache clearing.

    May be it helps for someone.

提交回复
热议问题