0

I am using Doctrine to generate and update my DB based on my Entities. Furthermore, I am using a MyISAM engine on my tables. To do so I have added my annotations like:

/** (...) @ORM\Table(name="user",options={"engine":"MyISAM", "charset"="utf8"}) */

The tables were generated as MyISAM normally, but when I try to update them now doctrine is trying to generate the FKs. Then I got:

 General error: 1215 Cannot add foreign key constraint

I know MyISAM does not support FKs, is there a way to tell doctrine to skip the creation of FKs?

I am using both orm:schema-tools:update (in DEV) and migrations (in PROD). Also I am using zf2.

diegopso
  • 457
  • 3
  • 11

2 Answers2

2

Based on this thread I came with a solution.

I added an event listener to the EM.

$em->getEventManager()->addEventSubscriber(new DropForeignKeysListener());

This event drop all FKs from the schema generated from the annotations, as:

use Doctrine\Common\EventSubscriber;
use Doctrine\ORM\Tools\ToolEvents;
use Doctrine\ORM\Tools\Event\GenerateSchemaEventArgs;

class DropForeignKeysListener implements EventSubscriber
{
    public function postGenerateSchema(GenerateSchemaEventArgs $args) {
        $schema = $args->getSchema();

        $table_names = $schema->getTableNames();
        foreach ($table_names as $table_name) {
            $table = $schema->getTable($table_name);
            $fks = $table->getForeignKeys();

            foreach ($fks as $fk => $obj) {
                $table->removeForeignKey($fk);
            }
        }
    }

    public function getSubscribedEvents() {
        return array(ToolEvents::postGenerateSchema);
    }

}
diegopso
  • 457
  • 3
  • 11
-1

The MyISAM storage engine doesn't support row-level locking, foreign keys and it doesn't even support transactions. Check out this question for a more detailed answer.

If you don't have any specific & valid reason to use MyISAM, i strongly recommend to use InnoDB instead.

You may also want to read this comparison between InnoDB and MyISAM.

edigu
  • 9,878
  • 5
  • 57
  • 80
  • I know that it does not support FKs (I maybe don't let it clear in the question), I just want to avoid the creation when I try to update the DB. Otherwise I have to dump the SQL and remove the creation statements manually. Also I can chage it, but now even if I do so I will keep curious about how shall I proced to avoid it. For sample, now I am thinking in use an event subscriber like here: [Telling Schema-Tool to ignore an entity](https://groups.google.com/forum/#!topic/doctrine-user/rwWXZ7faPsA). That is more close to the answer I am looking for. – diegopso Oct 05 '15 at 12:21