schema-manager.txt 4.26 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
A Schema Manager instance helps you with the abstraction of the generation of SQL assets such as Tables, Sequences, Foreign Keys and Indexes.

To retrieve the `SchemaManager` for your connection you can use the `getSchemaManager()` method:

    [php]
    $sm = $conn->getSchemaManager();

Now with the `SchemaManager` instance in `$em` you can use the available methods to learn about your database schema:

++ listDatabases()

Retrieve an array of databases on the configured connection:

    [php]
    $databases = $sm->listDatabases();

++ listSequences($database = null)

Retrieve an array of `Doctrine\DBAL\Schema\Sequence` instances that exist for a database:

    [php]
    $sequences = $sm->listSequences();

Or if you want to manually specify a database name:

    [php]
    $sequences = $sm->listSequences('dbname');

Now you can loop over the array inspecting each sequence object:

    [php]
    foreach ($sequences as $sequence) {
        echo $sequence->getName() . "\n";
    }

++ listTableColumns($tableName)

Retrieve an array of `Doctrine\DBAL\Schema\Column` instances that exist for the given table:

    [php]
    $columns = $sm->listTableColumns('user');

Now you can loop over the array inspecting each column object:

    [php]
    foreach ($columns as $column) {
        echo $column->getName() . ': ' . $column->getType() . "\n";
    }

++ listTableDetails($tableName)

Retrieve a single `Doctrine\DBAL\Schema\Table` instance that encapsulates all the details of the given table:

    [php]
    $table = $sm->listTableDetails('user');

Now you can call methods on the table to manipulate the in memory schema for that table. For example we can add a new column:

    [php]
    $table->addColumn('email_address', 'string');

++ listTableForeignKeys($tableName)

Retrieve an array of `Doctrine\DBAL\Schema\ForeignKeyConstraint` instances that exist for the given table:

    [php]
    $foreignKeys = $sm->listTableForeignKeys('user');

Now you can loop over the array inspecting each foreign key object:

    [php]
    foreach ($foreignKeys as $foreignKey) {
        echo $foreignKey->getName() . ': ' . $foreignKey->getLocalTableName() ."\n";
    }

++ listTableIndexes($tableName)

Retrieve an array of `Doctrine\DBAL\Schema\Index` instances that exist for the given table:

    [php]
    $indexes = $sm->listTableIndexes('user');

Now you can loop over the array inspecting each index object:

    [php]
    foreach ($indexes as $index) {
        echo $index->getName() . ': ' . ($index->isUnique() ? 'unique' : 'not unique') . "\n";
    }

++ listTables()

Retrieve an array of `Doctrine\DBAL\Schema\Table` instances that exist in the connections database:

    [php]
    $tables = $sm->listTables();

Each `Doctrine\DBAl\Schema\Table` instance is populated with information provided by all the above methods. So it encapsulates an array of `Doctrine\DBAL\Schema\Column` instances that can be retrieved with the `getColumns()` method:

    [php]
    foreach ($tables as $table) {
      echo $table->getName() . " columns:\n\n";
      foreach ($table->getColumns() as $column) {
        echo ' - ' . $column->getName() . "\n";
      }
    }


++ listViews()

Retrieve an array of `Doctrine\DBAL\Schema\View` instances that exist in the connections database:

    [php]
    $views = $sm->listViews();

Now you can loop over the array inspecting each view object:

    [php]
    foreach ($views as $view) {
        echo $view->getName() . ': ' . $view->getSql() . "\n";
    }

++ createSchema()

For a complete representation of the current database you can use the `createSchema()` method which returns an instance of `Doctrine\DBAL\Schema\Schema`, which you can use in conjunction with the SchemaTool or Schema Comparator.

    [php]
    $fromSchema = $sm->createSchema();

Now we can clone the `$fromSchema` to `$toSchema` and drop a table:

    [php]
    $toSchema = clone $fromSchema;
    $toSchema->dropTable('user');

Now we can compare the two schema instances in order to calculate the differences between them and return the sql required to make the changes on the database:

    [php]
138
    $sql = $fromSchema->getMigrateToSql($toSchema, $conn->getDatabasePlatform());
139 140 141 142 143

The `$sql` array should give you a sql query to drop the user table:

    [php]
    print_r($sql);
144

145 146 147 148 149
    /*
    array(
      0 => 'DROP TABLE user'
    )
    */