Ifsnop\Mysqldump\Mysqldump::start PHP Метод

start() публичный Метод

Main call
public start ( string $filename = '' ) : null
$filename string Name of file to write sql dump to
Результат null
    public function start($filename = '')
    {
        // Output file can be redefined here
        if (!empty($filename)) {
            $this->fileName = $filename;
        }
        // Connect to database
        $this->connect();
        // Create output file
        $this->compressManager->open($this->fileName);
        // Write some basic info to output file
        $this->compressManager->write($this->getDumpFileHeader());
        // Store server settings and use sanner defaults to dump
        $this->compressManager->write($this->typeAdapter->backup_parameters($this->dumpSettings));
        if ($this->dumpSettings['databases']) {
            $this->compressManager->write($this->typeAdapter->getDatabaseHeader($this->dbName));
            if ($this->dumpSettings['add-drop-database']) {
                $this->compressManager->write($this->typeAdapter->add_drop_database($this->dbName));
            }
        }
        // Get table, view and trigger structures from database
        $this->getDatabaseStructure();
        if ($this->dumpSettings['databases']) {
            $this->compressManager->write($this->typeAdapter->databases($this->dbName));
        }
        // If there still are some tables/views in include-tables array,
        // that means that some tables or views weren't found.
        // Give proper error and exit.
        // This check will be removed once include-tables supports regexps
        if (0 < count($this->dumpSettings['include-tables'])) {
            $name = implode(",", $this->dumpSettings['include-tables']);
            throw new Exception("Table (" . $name . ") not found in database");
        }
        $this->exportTables();
        $this->exportViews();
        $this->exportTriggers();
        $this->exportProcedures();
        // Restore saved parameters
        $this->compressManager->write($this->typeAdapter->restore_parameters($this->dumpSettings));
        // Write some stats to output file
        $this->compressManager->write($this->getDumpFileFooter());
        // Close output file
        $this->compressManager->close();
    }

Usage Example

 /**
  * This action will FULLY(!!) drop all tables of the current configuration, create a zip from the
  * provided $remoteDsn Database and import the remote database:
  *
  * ```
  * ./vendor/bin/luya exporter/database/remote-replace-local "mysql:host=localhost;dbname=REMOTE_DB_NAME" "USERNAME" "PASSWORD"
  * ```
  *
  * @param string $fromDsn
  * @param string $fromUsername
  * @param string $fromPassword
  */
 public function actionRemoteReplaceLocal($remoteDsn, $remoteUsername, $remotePassword)
 {
     if (YII_ENV_PROD || YII_ENV == 'prod') {
         throw new Exception("Its not possible to use remote-replace-local method in prod environment as it would remove the prod database env.");
     }
     $temp = tempnam(sys_get_temp_dir(), uniqid());
     $dump = new Mysqldump($remoteDsn, $remoteUsername, $remotePassword);
     $dump->start($temp);
     $tempBackup = tempnam(sys_get_temp_dir(), uniqid() . '-BACKUP-' . time());
     $dump = new Mysqldump(Yii::$app->db->dsn, Yii::$app->db->username, Yii::$app->db->password);
     $dump->start($tempBackup);
     $this->outputInfo("Temporary Backup File has been created: " . $tempBackup);
     if ($this->interactive) {
         if ($this->confirm('Are you sure you want to drop all local tables and replace them with the Remote Databse?')) {
             if ($this->confirm('Last Time: Really?')) {
                 foreach (Yii::$app->db->schema->getTableNames() as $name) {
                     Yii::$app->db->createCommand()->dropTable($name)->execute();
                 }
                 Yii::$app->db->createCommand()->setSql(file_get_contents($temp))->execute();
                 unlink($temp);
                 return $this->outputSuccess("The local database has been replace with the remote database.");
             }
         }
         unlink($temp);
         unlink($tempBackup);
         return $this->outputError('Abort by user input.');
     } else {
         foreach (Yii::$app->db->schema->getTableNames() as $name) {
             Yii::$app->db->createCommand()->dropTable($name)->execute();
         }
         Yii::$app->db->createCommand()->setSql(file_get_contents($temp))->execute();
         unlink($temp);
         return $this->outputSuccess("The local database has been replace with the remote database.");
     }
 }
All Usage Examples Of Ifsnop\Mysqldump\Mysqldump::start