Skip to content

Commit 9e038c6

Browse files
authored
Merge pull request #7064 from morozov/schema-definition-documentation
Update schema definition documentaion
2 parents ca0c027 + f9b15d6 commit 9e038c6

File tree

1 file changed

+75
-21
lines changed

1 file changed

+75
-21
lines changed

docs/en/reference/schema-representation.rst

Lines changed: 75 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,32 +12,86 @@ Metadata model. Up to very specific functionality of your database
1212
system this allows you to generate SQL code that makes your Domain
1313
model work.
1414

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

2221
.. code-block:: php
2322
2423
<?php
25-
$schema = new \Doctrine\DBAL\Schema\Schema();
26-
$myTable = $schema->createTable("my_table");
27-
$myTable->addColumn("id", "integer", ["unsigned" => true]);
28-
$myTable->addColumn("username", "string", ["length" => 32]);
29-
$myTable->setPrimaryKey(["id"]);
30-
$myTable->addUniqueIndex(["username"]);
31-
$myTable->setComment('Some comment');
32-
$schema->createSequence("my_table_seq");
33-
34-
$myForeign = $schema->createTable("my_foreign");
35-
$myForeign->addColumn("id", "integer");
36-
$myForeign->addColumn("user_id", "integer");
37-
$myForeign->addForeignKeyConstraint($myTable, ["user_id"], ["id"], ["onUpdate" => "CASCADE"]);
38-
39-
$queries = $schema->toSql($myPlatform); // get queries to create this schema.
40-
$dropSchema = $schema->toDropSql($myPlatform); // get queries to safely delete this schema.
24+
25+
use Doctrine\DBAL\Schema\Column;
26+
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
27+
use Doctrine\DBAL\Schema\ForeignKeyConstraint\ReferentialAction;
28+
use Doctrine\DBAL\Schema\Index;
29+
use Doctrine\DBAL\Schema\Index\IndexType;
30+
use Doctrine\DBAL\Schema\PrimaryKeyConstraint;
31+
use Doctrine\DBAL\Schema\Schema;
32+
use Doctrine\DBAL\Schema\Table;
33+
34+
$user = Table::editor()
35+
->setUnquotedName('user')
36+
->addColumn(
37+
Column::editor()
38+
->setUnquotedName('id')
39+
->setTypeName('integer')
40+
->setUnsigned(true)
41+
->create()
42+
)
43+
->addColumn(
44+
Column::editor()
45+
->setUnquotedName('username')
46+
->setTypeName('string')
47+
->setLength(32)
48+
->create()
49+
)
50+
->addPrimaryKeyConstraint(
51+
PrimaryKeyConstraint::editor()
52+
->setUnquotedColumnNames('id')
53+
->create()
54+
)
55+
->addIndex(
56+
Index::editor()
57+
->setUnquotedName('idx_username')
58+
->setUnquotedColumnNames('username')
59+
->setType(IndexType::UNIQUE)
60+
->create()
61+
)
62+
->setComment('User table')
63+
->create();
64+
65+
$post = Table::editor()
66+
->setUnquotedName('post')
67+
->addColumn(
68+
Column::editor()
69+
->setUnquotedName('id')
70+
->setTypeName('integer')
71+
->create()
72+
)
73+
->addColumn(
74+
Column::editor()
75+
->setUnquotedName('user_id')
76+
->setTypeName('integer')
77+
->create()
78+
)
79+
->addForeignKeyConstraint(
80+
ForeignKeyConstraint::editor()
81+
->setUnquotedName('fk_user_id')
82+
->setUnquotedReferencingColumnNames('user_id')
83+
->setUnquotedReferencedTableName('user')
84+
->setUnquotedReferencedColumnNames('id')
85+
->setOnUpdateAction(ReferentialAction::CASCADE)
86+
->create()
87+
)
88+
->create();
89+
90+
$schema = new Schema([$user, $post]);
91+
$schema->createSequence('my_table_seq');
92+
93+
$createSQL = $schema->toSql($myPlatform); // get queries to create this schema
94+
$dropSQL = $schema->toDropSql($myPlatform); // get queries to drop this schema
4195
4296
Now if you want to compare this schema with another schema, you can
4397
use the ``Comparator`` class to get instances of ``SchemaDiff``,

0 commit comments

Comments
 (0)