schema-representation.rst 6.17 KB
Newer Older
1 2
Schema-Representation
=====================
3

4 5 6 7 8 9 10 11 12 13
Doctrine has a very powerful abstraction of database schemas. It
offers an object-oriented representation of a database schema with
support for all the details of Tables, Sequences, Indexes and
Foreign Keys. These Schema instances generate a representation that
is equal for all the supported platforms. Internally this
functionality is used by the ORM Schema Tool to offer you create,
drop and update database schema methods from your Doctrine ORM
Metadata model. Up to very specific functionality of your database
system this allows you to generate SQL code that makes your Domain
model work.
14

15
You will be pleased to hear, that Schema representation is
Phil Davis's avatar
Phil Davis committed
16
completely decoupled from the Doctrine ORM though, that is you can
17 18 19 20 21
also use it in any other project to implement database migrations
or for SQL schema generation for any metadata model that your
application has. You can easily generate a Schema, as a simple
example shows:

22
.. code-block:: php
23 24

    <?php
25 26 27 28 29 30
    $schema = new \Doctrine\DBAL\Schema\Schema();
    $myTable = $schema->createTable("my_table");
    $myTable->addColumn("id", "integer", array("unsigned" => true));
    $myTable->addColumn("username", "string", array("length" => 32));
    $myTable->setPrimaryKey(array("id"));
    $myTable->addUniqueIndex(array("username"));
31
    $myTable->setComment('Some comment');
32
    $schema->createSequence("my_table_seq");
33

34 35 36 37
    $myForeign = $schema->createTable("my_foreign");
    $myForeign->addColumn("id", "integer");
    $myForeign->addColumn("user_id", "integer");
    $myForeign->addForeignKeyConstraint($myTable, array("user_id"), array("id"), array("onUpdate" => "CASCADE"));
38

39 40 41
    $queries = $schema->toSql($myPlatform); // get queries to create this schema.
    $dropSchema = $schema->toDropSql($myPlatform); // get queries to safely delete this schema.

42 43
Now if you want to compare this schema with another schema, you can
use the ``Comparator`` class to get instances of ``SchemaDiff``,
44
``TableDiff`` and ``ColumnDiff``, as well as information about other
45
foreign key, sequence and index changes.
46

47
.. code-block:: php
48 49

    <?php
50 51
    $comparator = new \Doctrine\DBAL\Schema\Comparator();
    $schemaDiff = $comparator->compare($fromSchema, $toSchema);
52

53 54 55
    $queries = $schemaDiff->toSql($myPlatform); // queries to get from one to another schema.
    $saveQueries = $schemaDiff->toSaveSql($myPlatform);

56
The Save Diff mode is a specific mode that prevents the deletion of
Phil Davis's avatar
Phil Davis committed
57
tables and sequences that might occur when making a diff of your
58 59 60 61 62
schema. This is often necessary when your target schema is not
complete but only describes a subset of your application.

All methods that generate SQL queries for you make much effort to
get the order of generation correct, so that no problems will ever
Phil Davis's avatar
Phil Davis committed
63
occur with missing links of foreign keys.
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
Schema Assets
-------------

A schema asset is considered any abstract atomic unit in a database such as schemas,
tables, indexes, but also sequences, columns and even identifiers.
The following chapter gives an overview of all available Doctrine 2
schema assets with short explanations on their context and usage.
All schema assets reside in the ``Doctrine\DBAL\Schema`` namespace.

.. note::

    This chapter is far from being completely documented.

Column
~~~~~~

Represents a table column in the database schema.
A column consists of a name, a type, portable options, commonly supported options and
vendors specific options.

Portable options
^^^^^^^^^^^^^^^^

The following options are considered to be fully portable across all database platforms:

-  **notnull** (boolean): Whether the column is nullable or not. Defaults to ``true``.
-  **default** (integer|string): The default value of the column if no value was specified.
   Defaults to ``null``.
-  **autoincrement** (boolean): Whether this column should use an autoincremented value if
   no value was specified. Only applies to Doctrine's ``smallint``, ``integer``
   and ``bigint`` types. Defaults to ``false``.
-  **length** (integer): The maximum length of the column. Only applies to Doctrine's
   ``string`` and ``binary`` types. Defaults to ``null`` and is evaluated to ``255``
   in the platform.
-  **fixed** (boolean): Whether a ``string`` or ``binary`` Doctrine type column has
   a fixed length. Defaults to ``false``.
-  **precision** (integer): The precision of a Doctrine ``decimal`` or ``float`` type
   column that determines the overall maximum number of digits to be stored (including scale).
   Defaults to ``10``.
-  **scale** (integer): The exact number of decimal digits to be stored in a Doctrine
   ``decimal`` or ``float`` type column. Defaults to ``0``.
-  **customSchemaOptions** (array): Additional options for the column that are
   supported by all vendors:

   -  **unique** (boolean): Whether to automatically add a unique constraint for the column.
      Defaults to ``false``.

Common options
^^^^^^^^^^^^^^

The following options are not completely portable but are supported by most of the
vendors:

-  **unsigned** (boolean): Whether a ``smallint``, ``integer`` or ``bigint`` Doctrine
   type column should allow unsigned values only. Supported by MySQL, SQL Anywhere
   and Drizzle. Defaults to ``false``.
-  **comment** (integer|string): The column comment. Supported by MySQL, PostgreSQL,
   Oracle, SQL Server, SQL Anywhere and Drizzle. Defaults to ``null``.

Vendor specific options
^^^^^^^^^^^^^^^^^^^^^^^

The following options are completely vendor specific and absolutely not portable:

-  **columnDefinition**: The custom column declaration SQL snippet to use instead
   of the generated SQL by Doctrine. Defaults to ``null``. This can useful to add
   vendor specific declaration information that is not evaluated by Doctrine
   (such as the ``ZEROFILL`` attribute on MySQL).
-  **customSchemaOptions** (array): Additional options for the column that are
   supported by some vendors but not portable:
135

136 137
   -  **charset** (string): The character set to use for the column. Currently only supported
      on MySQL and Drizzle.
138 139
   -  **collation** (string): The collation to use for the column. Supported by MySQL, PostgreSQL,
      Sqlite, SQL Server and Drizzle.
140 141
   -  **check** (string): The check constraint clause to add to the column.
      Defaults to ``null``.