Skip to content

Commit 5e35a64

Browse files
authored
Merge pull request #2 from moufmouf/inheritance
Adding the "extends" method to the table class.
2 parents 0377728 + df4279f commit 5e35a64

File tree

4 files changed

+57
-1
lines changed

4 files changed

+57
-1
lines changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,3 +182,21 @@ $table->column('uuid')->string(36)->primaryKey();
182182
$table->column('uuid')->then()
183183
->primaryKey(['uuid']);
184184
```
185+
186+
**Declare an inheritance relationship between 2 tables:**
187+
188+
In SQL, there is no notion of "inheritance" like with PHP objects.
189+
However, a common way to model inheritance is to write one table for the base class (containing the base columns/properties) and then one table per extended class containing the additional columns/properties.
190+
Each extended table has **a primary key that is also a foreign key pointing to the base table**.
191+
192+
```php
193+
$db->table('contacts')
194+
->id()
195+
->column('email')->string(50);
196+
197+
$db->table('users')
198+
->extends('contacts')
199+
->column('password')->string(50);
200+
```
201+
202+
The `extends` method will automatically create a primary key with the same name and same type as the extended table. It will also make sure this primary key is a foreign key pointing to the extended table.

src/FluidColumn.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ public function references(string $tableName, ?string $constraintName = null, st
217217
$referencedColumns = $table->getPrimaryKeyColumns();
218218

219219
if (count($referencedColumns) > 1) {
220-
throw new FluidSchemaException('You cannot reference a table with a premiray key on several columns using FluidSchema. Use DBAL Schema methods instead.');
220+
throw new FluidSchemaException('You cannot reference a table with a primary key on several columns using FluidSchema. Use DBAL Schema methods instead.');
221221
}
222222

223223
$referencedColumnName = $referencedColumns[0];

src/FluidTable.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,21 @@ public function timestamps(): FluidTable
9999
$this->column('updated_at')->datetimeImmutable();
100100
return $this;
101101
}
102+
103+
public function extends(string $tableName): FluidTable
104+
{
105+
$inheritedTable = $this->schema->getDbalSchema()->getTable($tableName);
106+
107+
$pks = $inheritedTable->getPrimaryKeyColumns();
108+
109+
if (count($pks) > 1) {
110+
throw new FluidSchemaException('You cannot inherit from a table with a primary key on several columns using FluidSchema. Use DBAL Schema methods instead.');
111+
}
112+
113+
$pkName = $pks[0];
114+
$pk = $inheritedTable->getColumn($pkName);
115+
116+
$this->column($pk->getName())->references($tableName)->primaryKey();
117+
return $this;
118+
}
102119
}

tests/FluidTableTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,27 @@ public function testTimestamps()
116116
}
117117
}
118118

119+
public function testInherits()
120+
{
121+
$schema = new Schema();
122+
$fluid = new FluidSchema($schema);
123+
124+
$contacts = $fluid->table('contacts');
125+
$contacts->id();
126+
127+
$fluid->table('users')->extends('contacts');
128+
129+
$dbalColumn = $schema->getTable('users')->getColumn('id');
130+
131+
$this->assertSame(Type::getType(Type::INTEGER), $dbalColumn->getType());
132+
$fks = $schema->getTable('users')->getForeignKeys();
133+
$this->assertCount(1, $fks);
134+
$fk = array_pop($fks);
135+
$this->assertSame('users', $fk->getLocalTableName());
136+
$this->assertSame('contacts', $fk->getForeignTableName());
137+
$this->assertSame(['id'], $fk->getLocalColumns());
138+
}
139+
119140
public function testGetDbalSchema()
120141
{
121142
$schema = new Schema();

0 commit comments

Comments
 (0)