Skip to content

Commit 0377728

Browse files
authored
Merge pull request #1 from moufmouf/feature/default_value
Adding the possibility to set the default value of a column
2 parents 5ad27f5 + 4f2581c commit 0377728

11 files changed

+20
-12
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,12 @@ $table->unique(['login', 'status']);
141141
$table->column('description')->string(50)->null();
142142
```
143143

144+
**Set the default value of a column:**
145+
146+
```php
147+
$table->column('enabled')->bool()->default(true);
148+
```
149+
144150
**Create a foreign key**
145151

146152
```php

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
"prefer-stable": true,
3232
"extra": {
3333
"branch-alias": {
34-
"dev-master": "1.0.x-dev"
34+
"dev-master": "1.1.x-dev"
3535
}
3636
}
3737
}

src/DefaultNamingStrategy.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
namespace TheCodingMachine\FluidSchema;
55

6-
76
use Doctrine\Common\Inflector\Inflector;
87

98
class DefaultNamingStrategy implements NamingStrategyInterface
@@ -49,4 +48,4 @@ private function toSingular($plural): string
4948

5049
return implode('_', $strs);
5150
}
52-
}
51+
}

src/FluidColumn.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
namespace TheCodingMachine\FluidSchema;
55

6-
76
use Doctrine\DBAL\Schema\Column;
87
use Doctrine\DBAL\Schema\Table;
98
use Doctrine\DBAL\Types\Type;

src/FluidColumnOptions.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
namespace TheCodingMachine\FluidSchema;
55

6-
76
use Doctrine\DBAL\Schema\Column;
87
use Doctrine\DBAL\Types\Type;
98

@@ -90,6 +89,12 @@ public function primaryKey(?string $indexName = null): FluidColumnOptions
9089
return $this;
9190
}
9291

92+
public function default($defaultValue): FluidColumnOptions
93+
{
94+
$this->column->setDefault($defaultValue);
95+
return $this;
96+
}
97+
9398
public function then(): FluidTable
9499
{
95100
return $this->fluidTable;
@@ -99,5 +104,4 @@ public function column($name): FluidColumn
99104
{
100105
return $this->fluidTable->column($name);
101106
}
102-
103107
}

src/FluidSchema.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,5 +77,4 @@ public function getDbalSchema(): Schema
7777
{
7878
return $this->schema;
7979
}
80-
8180
}

src/FluidSchemaException.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,4 @@
33

44
class FluidSchemaException extends \Exception
55
{
6-
7-
}
6+
}

src/NamingStrategyInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@ public function getJointureTableName(string $table1, string $table2): string;
2020
* @return string
2121
*/
2222
public function getForeignKeyColumnName(string $targetTable): string;
23-
}
23+
}

tests/FluidColumnOptionsTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ public function testOptions()
3535
$columnOptions->autoIncrement();
3636
$this->assertSame(true, $dbalColumn->getAutoincrement());
3737

38+
$columnOptions->default(42);
39+
$this->assertSame(42, $dbalColumn->getDefault());
40+
3841
$this->assertSame($posts, $columnOptions->then());
3942

4043
$columnOptions->column('bar');

tests/FluidColumnTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,4 +142,4 @@ public function testReferenceException()
142142
$this->expectException(FluidSchemaException::class);
143143
$users->column('country_id')->references('countries', 'myfk');
144144
}
145-
}
145+
}

0 commit comments

Comments
 (0)