使用 Phinx 截断 运行 种子文件之前的所有表
Truncate all tables before running seed files with Phinx
所以,我一直在使用 Phinx 创建迁移。我希望能够截断 运行 种子文件之前的所有表(148 个表)。我正在考虑先创建一个 运行 的种子文件,然后它会截断所有表。要注意的是,如果我们添加更多表,我不想更改此文件。我将如何去做这件事。也许做一个 Show tables 然后循环遍历它们,但不确定如何做到这一点。任何帮助都会很棒!这是我目前所拥有的。
<?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');
}
}
答案在这里
$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');
如果您有迁移 table,那么这也会截断 table。这行得通。
$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);
}
}
所以,我一直在使用 Phinx 创建迁移。我希望能够截断 运行 种子文件之前的所有表(148 个表)。我正在考虑先创建一个 运行 的种子文件,然后它会截断所有表。要注意的是,如果我们添加更多表,我不想更改此文件。我将如何去做这件事。也许做一个 Show tables 然后循环遍历它们,但不确定如何做到这一点。任何帮助都会很棒!这是我目前所拥有的。
<?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');
}
}
答案在这里
$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');
如果您有迁移 table,那么这也会截断 table。这行得通。
$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);
}
}