Dump Database

Dump Database -- Dumping a database to MDB2 XML

Dump Database

You can use dumpDatabase() to copy your database to a schema file. dumpDatabase() accepts a database definition array, for instance:

<?php
require_once 'MDB2/Schema.php';

$options = array(
    'log_line_break' => '<br>',
    'idxname_format' => '%s',
    'debug' => true,
    'quote_identifier' => true,
    'force_defaults' => false,
    'portability' => false
);
$dsn = 'mysql://root:@localhost/MDB2Example';

$schema =& MDB2_Schema::factory($dsn, $options);

if (PEAR::isError($schema)) {
    $error = $schema->getMessage();
} else {
    $dump_options = array(
      'output_mode' => 'file',
      'output' => 'schema.xml',
      'end_of_line' => "\n"
    );

    $definition = $schema->getDefinitionFromDatabase();
    if (PEAR::isError($definition)) {
      $error = $definition->getMessage();
    } else {
      $op = $schema->dumpDatabase($definition, $dump_options, MDB2_SCHEMA_DUMP_ALL);

      if (PEAR::isError($op)) {
          $error = $op->getMessage();
      }
  }
}

if (isset($error)) {
    var_dump($error);
}

$schema->disconnect();
?>

The first parameter is just the database definition array. The second parameter is the options where we choose to output to a file. The third option tells dumpDatabase() what to be dumped - either the structure, the data in the tables, or both. This is defined using the constants MDB2_SCHEMA_DUMP_STRUCTURE, MDB2_SCHEMA_DUMP_CONTENT and MDB2_SCHEMA_DUMP_ALL.

Some databases don't accept a text field with a default value. Given that, notice that $options['force_defaults'] has to be set to false when you want to create a field with the type text, as it is true by default.