Search code examples
phpsymfonydoctrinesymfony4symfony5

Symfony 5 - Doctrine with schema_filter not working


I try to ignore two entities when I execute the command line doctrine:schema:update --force in my project who is writing like this :

/**
 * @ORM\Entity(readOnly=true)
 * @ORM\Table(name="view_tableau_de_bord")
 */
class ViewTableauDeBord
{
    //...
}

In my doctrine.yaml configuration file:

doctrine:
dbal:
    default_connection: default

    connections:
        default:
            url: '%env(resolve:DATABASE_URL)%'
            driver: 'pdo_pgsql'
            server_version: '12'
            charset: utf8
            schema_filter: ~^(?!view_)~
        # ...

Doctrine keeps generating all entities while my views are in the schema_filter. Do you have an explanation about this ? It's my first time with this option in a project.

Configuration du projet:

  • Symfony 5.4.14
  • PHP 7.4.26
  • doctrine:orm: 2.13.3
  • doctrine/annotations: 1.13.3
  • doctrine/doctrine-bundle: 2.7.0
  • doctrine/doctrine-migrations-bundle: 3.2.2
  • symfony/doctrine-bridge: 5.4.14
  • doctrine/data-fixtures: 1.5.3

Solution

  • schema_filter

    schema_filter is not made to "filter" entity but to filter db table from doctrine awareness.

    Here is an example :
    Assuming you manually create a table that is updated from a custom raw php cronjob called view_booking_by_customer_per_year, this table is not used by your code in your project but is used for data analysis.

    This is a typical example where you do not want doctrine to generate a query like this each time you want to update your schema.

    DROP TABLE view_booking_by_customer_per_year; // NO I DONT WANT THIS
    

    So using schema_filter you can tell doctrine to ignore this table in his validation and update process.

    Try to create a random table using raw sql and use doctrine:schema:validate. It will show database is not in sync error. Once you put it in schema_filter, the error won't occur anymore.

    It work for doctrine:migration:diff and doctrine:schema:update

    schema_ignore_class

    But if you want to avoid an entity to be generated inside the database there is this from the link in the answer of Ernesto:

     schema_ignore_classes:
       - Reference\To\My\Class
       - Reference\To\My\OtherClass
    

    Only working since Doctrine 2.7 version. You can find the complete example here : Ignore a Doctrine2 Entity when running schema-manager update

    Use doctrine migration

    I strongly advise you to use doctrine:migration:diff then doctrine:migration:migrate instead of doctrine:schema:update to perform change on database. It's ok for local dev, but when in production it is a very bad practice.

    https://symfony.com/bundles/DoctrineMigrationsBundle/current/index.html