How to disable FOREIGN KEY CONSTRAINT when using Doctrine generate migration?

好久不见. 提交于 2020-01-02 07:06:28

问题


I often change/add some field in Entity and using bin/console make:migration to generate migrations, that's convenience just like in Rails or Django. But I do not need Foreign Key Constraint when I am using Doctrine Relationships(ManyToOne, OneToMany...).

I have to delete lines contained Foreign Key Constraint in generated migrations. But When I doing some changes with Entity, and run bin/console make:migration, It will add Foreign Key Constraint again, it is annoying.

I do not care about data consistency.

In Django model ForeignKey you can set db_constraint=False, so migration would not generate Foreign Key constraint.

Is there some similar setting in Doctrine?


回答1:


Doctrine does not support this.

If you are using a relational back-end and declare association mappings between your entities, the generated code will include the appropriate foreign keys.

You are not required to use that code. If the FKs does not exist Doctrine will continue working fine, as you've discovered.

I do not care about data consistency

Oh, to be young and care-fee. Good luck with that.




回答2:


Doctrine doesn't support this natively, but you can do that with an event listener on postGenerateSchema event.

// src/Doctrine/IgnoreFksListener.php

namespace App\Doctrine;

use Doctrine\ORM\Tools\Event\GenerateSchemaEventArgs;

/**
 * see http://kamiladryjanek.com/en/ignore-entity-or-table-when-running-doctrine2-schema-update-command/
 */
class IgnoreFksListener
{

    /**
     * Remove fks from Schema
     * This listener is called when the schema has been generated (from mapping data of entities)
     *
     * @param GenerateSchemaEventArgs $args
     */
    public function postGenerateSchema(GenerateSchemaEventArgs $args)
    {
        $schema = $args->getSchema();
        $em = $args->getEntityManager();

        foreach ($schema->getTables() as $table) {
            $fks = $table->getForeignKeys();
            foreach ($fks as $fk) {
                $table->removeForeignKey($fk->getName());
//              dump('removed FK '.$fk->getName().' from '.$tabel->getName().' pointing to '.$fk->getForeignTableName().'.['.implode(', ', $fk->getForeignColumns()).']');
            }
        }
    }
}

And you have to register the listener in services.yaml

    App\Doctrine\IgnoreFksListener:
        tags:
            - {name: doctrine.event_listener, event: postGenerateSchema }

here you can find another example http://kamiladryjanek.com/en/ignore-entity-or-table-when-running-doctrine2-schema-update-command/




回答3:


I solved this by overriding one doctrine class in symfony 4.3, it looks like this for me:

https://stackoverflow.com/a/58559273/9631974



来源:https://stackoverflow.com/questions/57455725/how-to-disable-foreign-key-constraint-when-using-doctrine-generate-migration

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!