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 33

    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:

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

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

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

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

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

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

Or if you want to manually specify a database name:

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

226
.. code-block:: php
227 228 229 230 231 232 233 234 235 236 237

    <?php
    print_r($sql);
    
    /*
    array(
      0 => 'DROP TABLE user'
    )
    */