Get Database Definition

Get Database Definition – Generating the definition array of a physical database or MDB2 XML

Introduction

All the internal work of MDB2_Schema is done over array structures, which we will call "definition array". Many methods, such as createDatabase, requires a definition array as input parameter, instead of a filename. Others take care of the conversion automatically, accepting both inputs.

For the cases when you need a definition array, there are two avaliable methods to manually generate the definition array, one for each schema source - either a database or a schema file. Obviously you can also write it by hand, it is not intended to be done this way though.

Get Database Definition

You can use getDefinitionFromDatabase() to get the definition array from an existing database.

<?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 {
    
// this method _attempts_ to get the defintition from the database
    // make sure you have tested it with your database to see if it
    // returns what you expect
    
$definition $schema->getDefinitionFromDatabase();

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

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

$schema->disconnect();
?>

Though you have to use the method with caution, if you use the method on a database created by hand. Some of the fields might be slightly different, but once you create your database using MDB2_Schema it is reliable and will return the same $definition every time.

Parse Database Definition

You can use parseDatabaseDefinitionFile() to get the definition array from a schema file.

<?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 {
    
$definition $schema->parseDatabaseDefinitionFile('schema.xml');

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

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

$schema->disconnect();
?>

Although the method accepts more parameters, only the first one is required.

Installation and example for the usage of MDB2_Schema (Previous) Dumping a database to MDB2 XML (Next)
Last updated: Wed, 22 Oct 2014 — Download Documentation
Do you think that something on this page is wrong? Please file a bug report or add a note.
View this page in:

User Notes:

There are no user contributed notes for this page.