Skip to content

Commit 86b05db

Browse files
committed
Merge pull request #4 from moufmouf/1.0
Modifying parent/children relationship method signatures to return more data
2 parents 3fee3fc + 3bb0f14 commit 86b05db

File tree

3 files changed

+29
-19
lines changed

3 files changed

+29
-19
lines changed

README.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,21 @@ Therefore, a "user" ID has to match a "contact", but a "contact" has not necessa
6161

6262
You can use `SchemaAnalyzer` to detect parent / child relationships.
6363

64+
- `getParentRelationship` takes a table name in parameter and returns the DBAL `ForeignKeyConstraint` representing
65+
the relationship between this table and its parent.
66+
- `getChildrenRelationships` takes a table name in parameter and returns an array of DBAL `ForeignKeyConstraint`
67+
representing the relationship between this table and its children.
68+
69+
6470
```php
65-
$parent = $schemaAnalyzer->getParentTable("user");
71+
$parentKeyConstraint = $schemaAnalyzer->getParentRelationship("user");
72+
/* @var $parentKeyConstraint ForeignKeyConstraint */
73+
$parent = $parentKeyConstraint->getForeignTableName();
6674
// This will return the "contact" table (as a string)
6775

68-
$children = $schemaAnalyzer->getChildrenTables("contact");
76+
$childrenKeyConstraints = $schemaAnalyzer->getChildrenRelationships("contact");
77+
/* @var $childrenKeyConstraints ForeignKeyConstraint[] */
78+
$children = array_map(function($item) { return $item->getLocalTableName(); }, $childrenKeyConstraints);
6979
// This will return an array of tables whose parent is contact: ["user"]
7080
```
7181

src/SchemaAnalyzer.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -461,12 +461,12 @@ private function isInheritanceRelationship(ForeignKeyConstraint $fk)
461461
*
462462
* @param string $tableName
463463
*
464-
* @return string|null
464+
* @return ForeignKeyConstraint|null
465465
*/
466-
public function getParentTable($tableName)
466+
public function getParentRelationship($tableName)
467467
{
468468
return $this->fromCache($this->cachePrefix.'_parent_'.$tableName, function () use ($tableName) {
469-
return $this->getParentTableWithoutCache($tableName);
469+
return $this->getParentRelationshipWithoutCache($tableName);
470470
});
471471
}
472472

@@ -477,18 +477,18 @@ public function getParentTable($tableName)
477477
*
478478
* @param string $tableName
479479
*
480-
* @return string|null
480+
* @return ForeignKeyConstraint|null
481481
*/
482-
private function getParentTableWithoutCache($tableName)
482+
private function getParentRelationshipWithoutCache($tableName)
483483
{
484484
$table = $this->getSchema()->getTable($tableName);
485485
foreach ($table->getForeignKeys() as $fk) {
486486
if ($this->isInheritanceRelationship($fk)) {
487-
return $fk->getForeignTableName();
487+
return $fk;
488488
}
489489
}
490490

491-
return;
491+
return null;
492492
}
493493

494494
/**
@@ -498,12 +498,12 @@ private function getParentTableWithoutCache($tableName)
498498
*
499499
* @param string $tableName
500500
*
501-
* @return string[]
501+
* @return ForeignKeyConstraint[]
502502
*/
503-
public function getChildrenTables($tableName)
503+
public function getChildrenRelationships($tableName)
504504
{
505505
return $this->fromCache($this->cachePrefix.'_children_'.$tableName, function () use ($tableName) {
506-
return $this->getChildrenTablesWithoutCache($tableName);
506+
return $this->getChildrenRelationshipsWithoutCache($tableName);
507507
});
508508
}
509509

@@ -514,9 +514,9 @@ public function getChildrenTables($tableName)
514514
*
515515
* @param string $tableName
516516
*
517-
* @return string[]
517+
* @return ForeignKeyConstraint[]
518518
*/
519-
private function getChildrenTablesWithoutCache($tableName)
519+
private function getChildrenRelationshipsWithoutCache($tableName)
520520
{
521521
$schema = $this->getSchema();
522522
$children = [];
@@ -526,7 +526,7 @@ private function getChildrenTablesWithoutCache($tableName)
526526
}
527527
foreach ($table->getForeignKeys() as $fk) {
528528
if ($fk->getForeignTableName() === $tableName && $this->isInheritanceRelationship($fk)) {
529-
$children[] = $fk->getLocalTableName();
529+
$children[] = $fk;
530530
}
531531
}
532532
}

tests/SchemaAnalyzerTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -473,10 +473,10 @@ public function testInheritanceRelationship()
473473
$this->assertCount(1, $fks);
474474
$this->assertEquals('id', $fks[0]->getLocalColumns()[0]);
475475

476-
$this->assertEquals('contact', $schemaAnalyzer->getParentTable('user'));
477-
$this->assertNull($schemaAnalyzer->getParentTable('contact'));
476+
$this->assertEquals('contact', $schemaAnalyzer->getParentRelationship('user')->getForeignTableName());
477+
$this->assertNull($schemaAnalyzer->getParentRelationship('contact'));
478478

479-
$this->assertEquals(['user'], $schemaAnalyzer->getChildrenTables('contact'));
480-
$this->assertEquals([], $schemaAnalyzer->getChildrenTables('user'));
479+
$this->assertEquals('user', $schemaAnalyzer->getChildrenRelationships('contact')[0]->getLocalTableName());
480+
$this->assertEquals([], $schemaAnalyzer->getChildrenRelationships('user'));
481481
}
482482
}

0 commit comments

Comments
 (0)