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