symfony 3 doctrine schema_filter not working

2019-08-22 04:48发布

问题:

I have created views on my db and the corresponding Entity. All seems to work fine but whenever I run

php bin/console doctrine:schema:validate

it will tell me that the mapping is fine, but not the db, as follows:

[Database] FAIL - The database schema is not in sync with the current mapping file.

Looking it up I found that one can configure DBAL to filter out tables from validation.

This is what I attempted on config.yml ( check last line on code below). The intention is to exclude tables whose name start with "view" from validation.

doctrine:
dbal:
    default_connection: default
    connections:
        default:      
            driver: pdo_mysql
            host: '%database_host%'
            port: '%database_port%'
            dbname: '%database_name%'
            user: '%database_user%'
            password: '%database_password%'
            charset: utf8mb4
            default_table_options:
                charset: utf8mb4
                collate: utf8mb4_unicode_ci
            schema_filter: ~^(?!view_)~

So, the schema_filter as per this documentation should filter that out, but it doesn't.

I checked a few other questions, including this

Any ideas? Thanks

回答1:

Message tell you that your mapping it's not the same as your database schema, you must update your database scheme. For Symfony3 command is

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

Actually, this command is incredibly powerful. It compares what your database should look like (based on the mapping information of your entities) with how it actually looks, and executes the SQL statements needed to update the database schema to where it should be

You can also use

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

to see SQL you need to run, but without the change database scheme.

When you run

php bin/console doctrine:schema:validate

Doctrine will check your mapping files and for you is ok. After that will check your schema. In that moment your parameter schema_filter tell Doctrine to ignore all tables in database which name start with view, but in your mapping files exist entity with table name view... and for that you getting error

[Database] FAIL - The database schema is not in sync with the current mapping file.

So the schema_filter is use to tell Doctrine to ignore tables in database and not to ignore entities.

To see when to use schema_filter imagine situation that you need custom tables in database with names that start with custom_ , in your files you don't have entities mapped with this tables and if you call

 php bin/console doctrine:migrations:diff

this will drop all custom tables, except if you in your config file tell Doctrine to ignore custom tables and you can do that if set up parameter

 schema_filter: ~^(?!custom_)~

Please check Doctrine documentation