Skip to content

Commit ef16dd2

Browse files
authored
Update working-with-existing-databases.markdown (#441)
* Update working-with-existing-databases.markdown * Update documentation/cookbook/working-with-existing-databases.markdown * Update working-with-existing-databases.markdown
1 parent 5bd9840 commit ef16dd2

File tree

1 file changed

+42
-15
lines changed

1 file changed

+42
-15
lines changed

documentation/cookbook/working-with-existing-databases.markdown

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,42 +5,69 @@ title: Working With Existing Databases
55

66
# Working With Existing Databases #
77

8-
The following topics are targeted for developers who already have a working database solution in place, but would like to use Propel to work with the data. For this case, Propel provides a number of command-line utilities helping with migrations of data and data structures.
8+
The following topics are targeted for developers who already have a working database solution in place, but would like to use Propel to work with the data. Propel provides command-line utilities to help migrate database structures into Propel's abstract schema format, enabling model generation and cross-database support.
99

1010
## Working with Database Structures ##
1111

12-
Propel uses an abstract XML schema file to represent databases (the [schema](/documentation/reference/schema.html)). Propel builds the SQL specific to a database based on this schema. Propel also provides a way to reverse-engineer the generic schema file based on database metadata.
12+
Propel uses an abstract XML schema file to represent databases (see the [XML Schema Format](/documentation/reference/schema.html)). Propel builds SQL for your target database based on this schema — and can also reverse-engineer the schema file based on existing database metadata.
1313

1414
### Creating an XML Schema from a DB Structure ###
1515

16-
To generate a schema file, create a new directory for your project & specify the connection information in your *configuration file* for that project. For example, to create a new project, `legacyapp`, follow these steps:
16+
Propel provides the `database:reverse` command to reverse-engineer your database into a `schema.xml` file. This is useful when integrating with a legacy database or starting a project based on an existing schema.
1717

18-
1. If your database has some third party application tables, you can [add](/documentation/reference/configuration-file.html#exclude-tables) them to the `exclude_tables` configuration node.
18+
To generate the schema:
19+
20+
1. If your database includes tables you don’t want to include (e.g. third-party ones), [exclude them](/documentation/reference/configuration-file.html#exclude-tables) in the configuration using `exclude_tables`.
1921

2022
2. Go to the `legacyapp` project directory anywhere on your filesystem:
2123

2224
```bash
2325
$ cd legacyapp
2426
```
2527

26-
3. Run the `reverse` task to generate the `schema.xml` specifying your database
27-
credentials:
28+
3. Run the `database:reverse` command with your DSN or connection name:
29+
30+
```bash
31+
propel database:reverse "mysql:host=localhost;dbname=db;user=root;password=pwd"
32+
```
33+
34+
The argument is a [PDO DSN](/documentation/reference/configuration-file.html#dsn), quoted to preserve special characters.
35+
36+
4. Review the console output for any errors or warnings, then inspect the generated `schema.xml` file.
37+
38+
5. The schema file will be located in the `generated-reversed-database/` directory by default. You can now proceed with model generation:
2839

2940
```bash
30-
$ propel reverse "mysql:host=localhost;dbname=db;user=root;password=pwd"
41+
propel model:build
3142
```
3243

33-
The given string is a DSN which will be passed to a PDO object. See the
34-
[Configuration reference](/documentation/reference/configuration-file.html#dsn)
35-
for further information.
44+
#### Additional Options ####
45+
46+
You can customize the behavior using the following options:
47+
48+
| Option | Description | Default |
49+
|--------------------|---------------------------------------------------------------------------------------------|--------------------------------|
50+
| `--output-dir` | Where to write the generated `schema.xml` | `generated-reversed-database` |
51+
| `--database-name` | Name to use in the schema XML `<database name="...">` | `default` or connection name |
52+
| `--schema-name` | Logical schema name used inside the generated file (helpful for multi-schema setups) | `schema` |
53+
| `--namespace` | PHP namespace to use for generated model classes (optional) ||
54+
55+
Example using options:
56+
57+
```bash
58+
propel database:reverse "mysql:host=localhost;dbname=db;user=root;password=pwd" \
59+
--output-dir=app/schema \
60+
--database-name=legacy \
61+
--schema-name=core \
62+
--namespace="Legacy\Models"
63+
```
64+
65+
#### Limitations ####
3666

37-
4. Pay attention to any errors/warnings issued during the task execution and then
38-
examine the generated `schema.xml` file to make any corrections needed.
67+
`database:reverse` does not reverse-engineer views, materialized views, or enum types in PostgreSQL. These structures are not currently supported by Propel as first-class schema objects. If you want to use a view within Propel, you can manually define it as a `<table>` with `skipSql="true"` to generate read-only model/query classes.
3968

40-
5. _You're done!_ Now you have a `schema.xml` file in the `legacyapp/generated-reversed-database` project
41-
directory. You can now run the default Propel build to generate all the classes.
4269

43-
The generated `schema.xml` file should be used as a guide, not a final answer. There are some datatypes that Propel may not be familiar with; also some datatypes are simply not supported by Propel (e.g. arrays in PostgreSQL). Unfamiliar datatypes will be reported as warnings and substituted with a default VARCHAR datatype.
70+
Note: database:reverse replaces the old reverse command in Propel 2.x. The alias reverse is still available but may be deprecated in future versions.
4471

4572
>**Tip**The reverse engineering classes may not be able to provide the same level of detail for all databases. In particular, metadata information for SQLite is often very basic since SQLite is a typeless database.
4673

0 commit comments

Comments
 (0)