Ignore entity when generating a migration on Symfony

Viewed 569

I have a mysql view and I am using it as an entity inside my project. But when generating a migration it tries to create a table. Is there a way to tell symfony to ignore that entity when generating a new migration?

/**
 * @ORM\Entity
 * @ORM\Table(name="mysql_view_table")
 */
class MysqlViewTable {}
3 Answers

You can use this configuration to ignore the table mysql_view_table:

doctrine:
    dbal:
        schema_filter: ~^(?!mysql_view_table)~

For more information, you can visite DoctrineMigrationsBundle doc:

I had the same problem in my Symfony 6 Project.

I have on one hand entities that reference tables and on the other hand entities that reference views.

Now when creating a new migration with php bin/console make:migration, the migration file always contained commands to create tables that are actually views.

Example:

$this->addSql('CREATE TABLE address_view (id ...)

The following solution worked for me:

  1. I created two subfolders in the App/Entity folder:
  • Tables
  • Views
  1. All entities referencing views go into the Views folder and all others into the Tables folder
  2. Under config/packages/doctrine.yaml the mapping directory has to be changed from "src/Entities" to "src/Entities/Tables". So it looks like this:
    auto_generate_proxy_classes: true
    naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
    auto_mapping: true
    mappings:
      App:
        is_bundle: false
        dir: "%kernel.project_dir%/src/Entity/Tables"
        prefix: 'App\Entity'
        alias: App`
  1. In the moved entity files the namespace must be adapted:
  • Entities in the Views folder: namespace App\Entity\Views
  • Entities in the Tables folder: namespace App\Entity\Tables In all files, e.g. Controller or FormTypes, in which the entities are used, the "use" statement must be adapted accordingly:
  • Example: use App\Entity\Example -> use App\Entity\Tables\Example

I hope this may help you.

Related