|
| 1 | +[](https://packagist.org/packages/mouf/schema-analyzer) |
| 2 | +[](https://packagist.org/packages/mouf/schema-analyzer) |
| 3 | +[](https://packagist.org/packages/mouf/schema-analyzer) |
1 | 4 | [](https://scrutinizer-ci.com/g/thecodingmachine/schema-analyzer/?branch=1.0)
|
2 | 5 | [](https://travis-ci.org/thecodingmachine/schema-analyzer)
|
3 | 6 | [](https://coveralls.io/github/thecodingmachine/schema-analyzer?branch=1.0)
|
@@ -26,6 +29,41 @@ You can install this package through Composer:
|
26 | 29 | The packages adheres to the [SemVer](http://semver.org/) specification, and there will be full backward compatibility
|
27 | 30 | between minor versions.
|
28 | 31 |
|
29 |
| -## Usage |
| 32 | +## Detecting junction tables |
30 | 33 |
|
31 |
| -Start by instanciating |
| 34 | +The starting point is always a DBAL Schema. Pass the schema to SchemaAnalyzer, and then, simply call the functions. |
| 35 | + |
| 36 | +```php |
| 37 | +// $conn is the DBAL connection. |
| 38 | +$schema = $conn->getSchemaManager()->createSchema(); |
| 39 | + |
| 40 | +// Let's detect all junctions tables |
| 41 | +$tables = $schema->detectJunctionTables(); |
| 42 | +// This will return an array of Doctrine\DBAL\Schema\Table objects |
| 43 | +``` |
| 44 | + |
| 45 | +A **junction table** is a table: |
| 46 | + |
| 47 | +- that has **exactly 2 foreign keys** |
| 48 | +- that has **only 2 columns** (or **3 columns** if the one of those is an *autoincremented primary key*). |
| 49 | + |
| 50 | +## Computing the shortest path between 2 tables |
| 51 | + |
| 52 | +Following foreign keys, the `getShortestPath` function will try to find the shortest path between 2 tables. |
| 53 | +It will return the list of foreign keys it used to link the 2 tables. |
| 54 | + |
| 55 | +Internals: |
| 56 | + |
| 57 | +- Each foreign key has a *cost* of 1 |
| 58 | +- Junction tables have a cost of 1.5, instead of 2 (one for each foreign key) |
| 59 | + |
| 60 | +```php |
| 61 | +// $conn is the DBAL connection. |
| 62 | +$schema = $conn->getSchemaManager()->createSchema(); |
| 63 | + |
| 64 | +// Let's detect the shortest path between 2 tables: |
| 65 | +$fks = $schema->getShortestPath("users", "rights"); |
| 66 | +// This will return an array of Doctrine\DBAL\Schema\ForeignKeyConstraint objects |
| 67 | +``` |
| 68 | + |
| 69 | +// TODO: Ambiguity exception! |
0 commit comments