[英]Generate Database MyISAM Doctrine 2
I am using Doctrine to generate and update my DB based on my Entities. 我正在使用Doctrine根据我的实体生成和更新数据库。 Furthermore, I am using a MyISAM engine on my tables. 此外,我在表上使用MyISAM引擎。 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. 这些表通常是作为MyISAM生成的,但是当我尝试更新它们时,现在学说正在尝试生成FK。 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? 我知道MyISAM不支持FK,有没有办法告诉学说跳过FK的创建?
I am using both orm:schema-tools:update (in DEV) and migrations (in PROD). 我同时使用orm:schema-tools:update (在DEV中)和迁移 (在PROD中)。 Also I am using zf2. 我也在使用zf2。
Based on this thread I came with a solution. 基于此线程,我提供了一个解决方案。
I added an event listener to the EM. 我向EM添加了事件侦听器。
$em->getEventManager()->addEventSubscriber(new DropForeignKeysListener());
This event drop all FKs from the schema generated from the annotations, as: 此事件从由注释生成的架构中删除所有FK,如下所示:
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);
}
}
The MyISAM storage engine doesn't support row-level locking, foreign keys and it doesn't even support transactions. MyISAM存储引擎不支持行级锁定,外键,甚至不支持事务。 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. 如果您没有使用MyISAM的任何特定且有效的理由,我强烈建议您改用InnoDB。
You may also want to read this comparison between InnoDB and MyISAM . 您可能还需要阅读InnoDB和MyISAM之间的比较 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.