schema-manager.rst 5.24 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
Schema-Manager
==============

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:

11
.. code-block:: php
12 13 14 15

    <?php
    $sm = $conn->getSchemaManager();

Mohammad Shokri's avatar
Mohammad Shokri committed
16
Now with the ``SchemaManager`` instance in ``$sm`` you can use the
17 18
available methods to learn about your database schema:

Benjamin Eberlei's avatar
Benjamin Eberlei committed
19
.. note::
20 21 22 23 24 25 26 27 28 29 30 31 32

    Parameters containing identifiers passed to the SchemaManager
    methods are *NOT* quoted automatically! Identifier quoting is
    really difficult to do manually in a consistent way across
    different databases. You have to manually quote the identifiers
    when you accept data from user- or other sources not under your
    control.

listDatabases()
---------------

Retrieve an array of databases on the configured connection:

33
.. code-block:: php
34 35 36 37

    <?php
    $databases = $sm->listDatabases();

Benjamin Eberlei's avatar
Benjamin Eberlei committed
38
listSequences()
39 40 41 42 43
-------------------------------

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

44
.. code-block:: php
45 46 47 48 49 50

    <?php
    $sequences = $sm->listSequences();

Or if you want to manually specify a database name:

51
.. code-block:: php
52 53 54 55 56 57

    <?php
    $sequences = $sm->listSequences('dbname');

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

58
.. code-block:: php
59 60 61 62 63 64

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

Benjamin Eberlei's avatar
Benjamin Eberlei committed
65
listTableColumns()
66 67 68 69 70
----------------------------

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

71
.. code-block:: php
72 73 74 75 76 77

    <?php
    $columns = $sm->listTableColumns('user');

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

78
.. code-block:: php
79 80 81 82 83 84

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

Benjamin Eberlei's avatar
Benjamin Eberlei committed
85
listTableDetails()
86 87 88 89 90
----------------------------

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

91
.. code-block:: php
92 93 94 95 96 97 98

    <?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:

99
.. code-block:: php
100 101 102 103

    <?php
    $table->addColumn('email_address', 'string');

Benjamin Eberlei's avatar
Benjamin Eberlei committed
104
listTableForeignKeys()
105 106 107 108 109
--------------------------------

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

110
.. code-block:: php
111 112 113 114 115 116 117

    <?php
    $foreignKeys = $sm->listTableForeignKeys('user');

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

118
.. code-block:: php
119 120 121 122 123 124

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

Benjamin Eberlei's avatar
Benjamin Eberlei committed
125
listTableIndexes()
126 127 128 129 130
----------------------------

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

131
.. code-block:: php
132 133 134 135 136 137

    <?php
    $indexes = $sm->listTableIndexes('user');

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

138
.. code-block:: php
139 140 141 142 143 144 145 146 147 148 149 150

    <?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:

151
.. code-block:: php
152 153 154 155 156 157 158 159 160

    <?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:

161
.. code-block:: php
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176

    <?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:

177
.. code-block:: php
178 179 180 181 182 183

    <?php
    $views = $sm->listViews();

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

184
.. code-block:: php
185 186 187 188 189 190 191 192 193 194 195 196 197 198

    <?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.

199
.. code-block:: php
200 201 202 203 204 205 206

    <?php
    $fromSchema = $sm->createSchema();

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

207
.. code-block:: php
208 209 210 211 212 213

    <?php
    $toSchema = clone $fromSchema;
    $toSchema->dropTable('user');

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

217
.. code-block:: php
218 219 220 221

    <?php
    $sql = $fromSchema->getMigrateToSql($toSchema, $conn->getDatabasePlatform());

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

225
.. code-block:: php
226 227 228

    <?php
    print_r($sql);
229

230 231 232 233 234 235
    /*
    array(
      0 => 'DROP TABLE user'
    )
    */