0

所以,我一直在使用 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');
    }
}
4

2 回答 2

2

If you have a migrations table, then this will truncate that table as well. This would work.

 $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);
            }
        }
于 2017-04-18T22:43:31.397 回答
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');
于 2017-03-15T21:14:50.980 回答