所以,我一直在使用Phinx创建迁移。我希望能够在运行种子文件之前截断所有表(148个表)。我正在考虑创建一个将首先运行的种子文件,然后它将截断所有表。问题是如果我们添加更多表格,我不想更改此文件。我该怎么做呢也许做一个显示表然后循环它们,但不完全确定如何做到这一点。任何帮助都会很棒!这是我到目前为止所做的。
<?php
use Phinx\Seed\AbstractSeed;
class BonusRuleTypesSeeder extends AbstractSeed
{
public function run()
{
$this->execute('SET foreign_key_checks=0');
// some code here
$this->execute('SET foreign_key_checks=1');
}
}
答案 0 :(得分:1)
如果您有迁移表,那么这也会截断该表。这样可行。
$this->execute('SET foreign_key_checks=0');
foreach($tables as $table){
$table = $table["Tables_in_".$database];
if ($table != $config->getProperty(['phinx', 'default_migration_table'])){
$sql = "TRUNCATE TABLE ".$table;
$this->execute($sql);
}
}
答案 1 :(得分:0)
以下是答案
$config = new Config(__DIR__.'/../../config/default.ini',true);
$host = $config->getProperty(['db', 'host']);
$database = $config->getProperty(['db', 'name']);
$username = $config->getProperty(['db', 'username']);
$password = $config->getProperty(['db', 'password']);
$mysqli = new mysqli($host, $username, $password, $database);
$query = "Show tables";
$tables = $mysqli->query($query);
$tables->fetch_all();
$mysqli->close();
$this->execute('SET foreign_key_checks=0');
foreach($tables as $table){
$table = $table["Tables_in_".$database];
$sql = "TRUNCATE TABLE ".$table;
$this->execute($sql);
}
$this->execute('SET foreign_key_checks=1');