1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace TheCodingMachine\FluidSchema; |
5
|
|
|
|
6
|
|
|
use Doctrine\DBAL\Schema\Table; |
7
|
|
|
|
8
|
|
|
class FluidTable |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var FluidSchema |
12
|
|
|
*/ |
13
|
|
|
private $schema; |
14
|
|
|
/** |
15
|
|
|
* @var Table |
16
|
|
|
*/ |
17
|
|
|
private $table; |
18
|
|
|
/** |
19
|
|
|
* @var FluidColumn[] |
20
|
|
|
*/ |
21
|
|
|
private $fluidColumns; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param FluidSchema $schema |
25
|
|
|
* @param Table $table |
26
|
|
|
*/ |
27
|
|
|
public function __construct(FluidSchema $schema, Table $table) |
28
|
|
|
{ |
29
|
|
|
$this->schema = $schema; |
30
|
|
|
$this->table = $table; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function column(string $name): FluidColumn |
34
|
|
|
{ |
35
|
|
|
if (isset($this->fluidColumns[$name])) { |
36
|
|
|
return $this->fluidColumns[$name]; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
if ($this->table->hasColumn($name)) { |
40
|
|
|
$column = $this->table->getColumn($name); |
41
|
|
|
} else { |
42
|
|
|
$column = $this->table->addColumn($name, 'string'); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$this->fluidColumns[$name] = new FluidColumn($this->schema, $this, $this->table, $column); |
46
|
|
|
return $this->fluidColumns[$name]; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function index(array $columnNames): FluidTable |
50
|
|
|
{ |
51
|
|
|
$this->table->addIndex($columnNames); |
52
|
|
|
return $this; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function unique(array $columnNames): FluidTable |
56
|
|
|
{ |
57
|
|
|
$this->table->addUniqueIndex($columnNames); |
58
|
|
|
return $this; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function primaryKey(array $columnNames, ?string $indexName = null): FluidTable |
62
|
|
|
{ |
63
|
|
|
$newIndexName = $indexName ?: false; |
64
|
|
|
|
65
|
|
|
$this->table->setPrimaryKey($columnNames, $newIndexName); |
66
|
|
|
return $this; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Creates a "id" autoincremented primary key column. |
71
|
|
|
* |
72
|
|
|
* @return FluidTable |
73
|
|
|
*/ |
74
|
|
|
public function id(): FluidTable |
75
|
|
|
{ |
76
|
|
|
$this->column('id')->integer()->primaryKey()->autoIncrement(); |
77
|
|
|
return $this; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Creates a "uuid" primary key column. |
82
|
|
|
* |
83
|
|
|
* @return FluidTable |
84
|
|
|
*/ |
85
|
|
|
public function uuid(): FluidTable |
86
|
|
|
{ |
87
|
|
|
$this->column('uuid')->guid()->primaryKey(); |
88
|
|
|
return $this; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Creates "created_at" and "updated_at" columns. |
93
|
|
|
* |
94
|
|
|
* @return FluidTable |
95
|
|
|
*/ |
96
|
|
|
public function timestamps(): FluidTable |
97
|
|
|
{ |
98
|
|
|
$this->column('created_at')->datetimeImmutable(); |
99
|
|
|
$this->column('updated_at')->datetimeImmutable(); |
100
|
|
|
return $this; |
101
|
|
|
} |
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
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|