2017-03-10 1 views
0

그래서 Phxx를 사용하여 마이그레이션을 생성했습니다. 시드 파일을 실행하기 전에 모든 테이블 (148 테이블)을자를 수 있기를 원합니다. 먼저 실행될 시드 파일을 만든 다음 모든 테이블을 잘라내려고 생각하고 있습니다. 캐치는 더 많은 테이블을 추가하면이 파일을 변경하지 않아도된다는 것을 원하지 않는다는 것입니다. 이 일에 대해 어떻게 생각 하나? 어쩌면 Show 테이블을 실행 한 다음 그 테이블을 반복하면서 그 작업을 수행하는 방법을 정확히 알지 못합니다. 어떤 도움이라도 좋을 것입니다! 이것은 내가 지금까지 가지고있는 것이다. 당신이 마이그레이션 테이블이있는 경우Phinx로 시드 파일을 실행하기 전에 모든 테이블을 잘라냅니다.

<?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'); 
    } 
} 

답변

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); 
      } 
     } 
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'); 
관련 문제