1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LaravelDoctrine\Fluent\Builders; |
4
|
|
|
|
5
|
|
|
use BadMethodCallException; |
6
|
|
|
use Doctrine\DBAL\Types\Type; |
7
|
|
|
use Doctrine\ORM\Mapping\Builder\ClassMetadataBuilder; |
8
|
|
|
use Doctrine\ORM\Mapping\Builder\FieldBuilder; |
9
|
|
|
use Doctrine\ORM\Mapping\ClassMetadataInfo; |
10
|
|
|
use LaravelDoctrine\Fluent\Buildable; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @method $this unique(boolean $flag = true) Boolean value to determine if the value of the column should be unique |
14
|
|
|
* across all rows of the underlying entities table. |
15
|
|
|
* @method $this nullable(boolean $flag = true) Determines if NULL values are allowed for this column. |
16
|
|
|
* @method $this length(int $length) Used by the “string” type to determine its maximum length in the |
17
|
|
|
* database. Doctrine does not validate the length of string values |
18
|
|
|
* for you. |
19
|
|
|
* @method $this columnName(string $column) By default the property name is used for the database column name also, |
20
|
|
|
* however the ‘name’ attribute allows you to determine the column name. |
21
|
|
|
* @method $this precision(int $precision) The precision for a decimal (exact numeric) column (applies only for |
22
|
|
|
* decimal column), which is the maximum number of digits that are stored |
23
|
|
|
* for the values. |
24
|
|
|
* @method $this scale(int $scale) The scale for a decimal (exact numeric) column (applies only for |
25
|
|
|
* decimal column), which represents the number of digits to the right of |
26
|
|
|
* the decimal point and must not be greater than precision. |
27
|
|
|
* @method $this default(string $default) The default value to set for the column if no value is supplied. |
28
|
|
|
* @method $this columnDefinition(string $def) DDL SQL snippet that starts after the column name and specifies the |
29
|
|
|
* complete (non-portable!) column definition. This attribute allows to |
30
|
|
|
* make use of advanced RMDBS features. However you should make careful |
31
|
|
|
* use of this feature and the consequences. SchemaTool will not detect |
32
|
|
|
* changes on the column correctly anymore if you use “columnDefinition”. |
33
|
|
|
* @method $this option($name, $value) Set custom options |
34
|
|
|
*/ |
35
|
|
|
class Field implements Buildable |
36
|
|
|
{ |
37
|
|
|
/** |
38
|
|
|
* @var FieldBuilder |
39
|
|
|
*/ |
40
|
|
|
protected $builder; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var ClassMetadataInfo |
44
|
|
|
*/ |
45
|
|
|
protected $classMetadata; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Protected constructor to force usage of factory method |
49
|
|
|
* |
50
|
|
|
* @param FieldBuilder $builder |
51
|
|
|
* @param ClassMetadataInfo $classMetadata |
52
|
|
|
*/ |
53
|
64 |
|
protected function __construct(FieldBuilder $builder, ClassMetadataInfo $classMetadata) |
54
|
|
|
{ |
55
|
64 |
|
$this->builder = $builder; |
56
|
64 |
|
$this->classMetadata = $classMetadata; |
57
|
64 |
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param ClassMetadataBuilder $builder |
61
|
|
|
* @param string $type |
62
|
|
|
* @param string $name |
63
|
|
|
* |
64
|
|
|
* @throws \Doctrine\DBAL\DBALException |
65
|
|
|
* @return Field |
66
|
|
|
*/ |
67
|
64 |
|
public static function make(ClassMetadataBuilder $builder, $type, $name) |
68
|
|
|
{ |
69
|
64 |
|
$type = Type::getType($type); |
70
|
|
|
|
71
|
64 |
|
$field = $builder->createField($name, $type->getName()); |
72
|
|
|
|
73
|
64 |
|
return new static($field, $builder->getClassMetadata()); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* By default the property name is used for the database column name also, however the ‘name’ attribute |
78
|
|
|
* allows you to determine the column name. |
79
|
|
|
* |
80
|
|
|
* @param string $columnName |
81
|
|
|
* |
82
|
|
|
* @return $this |
83
|
|
|
*/ |
84
|
6 |
|
public function name($columnName) |
85
|
|
|
{ |
86
|
6 |
|
$this->columnName($columnName); |
87
|
|
|
|
88
|
6 |
|
return $this; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @return Field |
93
|
|
|
*/ |
94
|
7 |
|
public function autoIncrement() |
95
|
|
|
{ |
96
|
7 |
|
$this->generatedValue(); |
97
|
|
|
|
98
|
7 |
|
return $this; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param callable|null $callback |
103
|
|
|
* |
104
|
|
|
* @return Field |
105
|
|
|
*/ |
106
|
10 |
|
public function generatedValue(callable $callback = null) |
107
|
|
|
{ |
108
|
10 |
|
$generatedValue = new GeneratedValue($this->builder, $this->classMetadata); |
109
|
|
|
|
110
|
10 |
|
if ($callback) { |
111
|
2 |
|
$callback($generatedValue); |
112
|
2 |
|
} |
113
|
|
|
|
114
|
10 |
|
$generatedValue->build(); |
115
|
|
|
|
116
|
10 |
|
return $this; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Boolean value to determine if the column should be capable of representing only non-negative integers |
121
|
|
|
* (applies only for integer column and might not be supported by all vendors). |
122
|
|
|
* |
123
|
|
|
* @return Field |
124
|
|
|
*/ |
125
|
10 |
|
public function unsigned() |
126
|
|
|
{ |
127
|
10 |
|
$this->builder->option('unsigned', true); |
128
|
|
|
|
129
|
10 |
|
return $this; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Boolean value to determine if the specified length of a string column should be fixed or varying |
134
|
|
|
* (applies only for string/binary column and might not be supported by all vendors). |
135
|
|
|
* |
136
|
|
|
* @param bool $fixed |
137
|
|
|
* |
138
|
|
|
* @return Field |
139
|
|
|
*/ |
140
|
1 |
|
public function fixed($fixed) |
141
|
|
|
{ |
142
|
1 |
|
$this->builder->option('fixed', $fixed); |
143
|
|
|
|
144
|
1 |
|
return $this; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* The comment of the column in the schema (might not be supported by all vendors). |
149
|
|
|
* |
150
|
|
|
* @param string $comment |
151
|
|
|
* |
152
|
|
|
* @return Field |
153
|
|
|
*/ |
154
|
1 |
|
public function comment($comment) |
155
|
|
|
{ |
156
|
1 |
|
$this->builder->option('comment', $comment); |
157
|
|
|
|
158
|
1 |
|
return $this; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* The collation of the column (only supported by Drizzle, Mysql, PostgreSQL>=9.1, Sqlite and SQLServer). |
163
|
|
|
* |
164
|
|
|
* @param string $collation |
165
|
|
|
* |
166
|
|
|
* @return Field |
167
|
|
|
*/ |
168
|
1 |
|
public function collation($collation) |
169
|
|
|
{ |
170
|
1 |
|
$this->builder->option('collation', $collation); |
171
|
|
|
|
172
|
1 |
|
return $this; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @return Field |
177
|
|
|
*/ |
178
|
8 |
|
public function primary() |
179
|
|
|
{ |
180
|
8 |
|
$this->builder->makePrimaryKey(); |
181
|
|
|
|
182
|
8 |
|
return $this; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @return Field |
187
|
|
|
*/ |
188
|
21 |
|
public function useForVersioning() |
189
|
|
|
{ |
190
|
21 |
|
$this->builder->isVersionField(); |
191
|
|
|
|
192
|
21 |
|
return $this; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* @return Field |
197
|
|
|
*/ |
198
|
49 |
|
public function build() |
199
|
|
|
{ |
200
|
49 |
|
$this->builder->build(); |
201
|
|
|
|
202
|
33 |
|
return $this; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* @return FieldBuilder |
207
|
|
|
*/ |
208
|
20 |
|
public function getBuilder() |
209
|
|
|
{ |
210
|
20 |
|
return $this->builder; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Magic call method works as a proxy for the Doctrine FieldBuilder |
215
|
|
|
* |
216
|
|
|
* @param string $method |
217
|
|
|
* @param array $args |
218
|
|
|
* |
219
|
|
|
* @throws BadMethodCallException |
220
|
|
|
* @return $this |
221
|
|
|
*/ |
222
|
14 |
|
public function __call($method, $args) |
223
|
|
|
{ |
224
|
|
|
// Work around reserved keywords |
225
|
14 |
|
if ($method === 'default') { |
226
|
1 |
|
return call_user_func_array([$this, 'setDefault'], $args); |
227
|
|
|
} |
228
|
|
|
|
229
|
13 |
|
if (method_exists($this->getBuilder(), $method)) { |
230
|
12 |
|
call_user_func_array([$this->getBuilder(), $method], $args); |
231
|
|
|
|
232
|
12 |
|
return $this; |
233
|
|
|
} |
234
|
|
|
|
235
|
1 |
|
throw new BadMethodCallException("FieldBuilder method [{$method}] does not exist."); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* The default value to set for the column if no value is supplied. |
240
|
|
|
* |
241
|
|
|
* @param string $default |
242
|
|
|
* |
243
|
|
|
* @return Field |
244
|
|
|
*/ |
245
|
1 |
|
protected function setDefault($default) |
246
|
|
|
{ |
247
|
1 |
|
$this->builder->option('default', $default); |
248
|
|
|
|
249
|
1 |
|
return $this; |
250
|
|
|
} |
251
|
|
|
} |
252
|
|
|
|