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
|
300 |
|
protected function __construct(FieldBuilder $fieldBuilder, ClassMetadataBuilder $builder, Type $type, $name) |
84
|
|
|
{ |
85
|
300 |
|
$this->fieldBuilder = $fieldBuilder; |
86
|
300 |
|
$this->metaDatabuilder = $builder; |
87
|
300 |
|
$this->classMetadata = $builder->getClassMetadata(); |
88
|
300 |
|
$this->type = $type; |
89
|
300 |
|
$this->name = $name; |
90
|
300 |
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param ClassMetadataBuilder $builder |
94
|
|
|
* @param string $type |
95
|
|
|
* @param string $name |
96
|
|
|
* |
97
|
|
|
* @throws \Doctrine\DBAL\DBALException |
98
|
|
|
* @return Field |
99
|
|
|
*/ |
100
|
300 |
|
public static function make(ClassMetadataBuilder $builder, $type, $name) |
101
|
|
|
{ |
102
|
300 |
|
$type = Type::getType($type); |
103
|
|
|
|
104
|
300 |
|
$field = $builder->createField($name, $type->getName()); |
105
|
|
|
|
106
|
300 |
|
return new static($field, $builder, $type, $name); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @return string |
111
|
|
|
*/ |
112
|
106 |
|
public function getName() |
113
|
|
|
{ |
114
|
106 |
|
return $this->name; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @return Type |
119
|
|
|
*/ |
120
|
1 |
|
public function getType() |
121
|
|
|
{ |
122
|
1 |
|
return $this->type; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @return ClassMetadata|ExtensibleClassMetadata |
127
|
|
|
*/ |
128
|
104 |
|
public function getClassMetadata() |
129
|
|
|
{ |
130
|
104 |
|
return $this->classMetadata; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* By default the property name is used for the database column name also, however the ‘name’ attribute |
135
|
|
|
* allows you to determine the column name. |
136
|
|
|
* |
137
|
|
|
* @param string $columnName |
138
|
|
|
* |
139
|
|
|
* @return $this |
140
|
|
|
*/ |
141
|
6 |
|
public function name($columnName) |
142
|
|
|
{ |
143
|
6 |
|
$this->columnName($columnName); |
144
|
|
|
|
145
|
6 |
|
return $this; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @return Field |
150
|
|
|
*/ |
151
|
7 |
|
public function autoIncrement() |
152
|
|
|
{ |
153
|
7 |
|
$this->generatedValue(); |
154
|
|
|
|
155
|
7 |
|
return $this; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @param callable|null $callback |
160
|
|
|
* |
161
|
|
|
* @return Field |
162
|
|
|
*/ |
163
|
10 |
|
public function generatedValue(callable $callback = null) |
164
|
|
|
{ |
165
|
10 |
|
$generatedValue = new GeneratedValue($this->fieldBuilder, $this->classMetadata); |
166
|
|
|
|
167
|
10 |
|
if ($callback) { |
168
|
2 |
|
$callback($generatedValue); |
169
|
2 |
|
} |
170
|
|
|
|
171
|
10 |
|
$generatedValue->build(); |
172
|
|
|
|
173
|
10 |
|
return $this; |
174
|
4 |
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Boolean value to determine if the column should be capable of representing only non-negative integers |
178
|
|
|
* (applies only for integer column and might not be supported by all vendors). |
179
|
|
|
* |
180
|
|
|
* @return Field |
181
|
|
|
*/ |
182
|
10 |
|
public function unsigned() |
183
|
|
|
{ |
184
|
10 |
|
$this->fieldBuilder->option('unsigned', true); |
185
|
|
|
|
186
|
10 |
|
return $this; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Boolean value to determine if the specified length of a string column should be fixed or varying |
191
|
|
|
* (applies only for string/binary column and might not be supported by all vendors). |
192
|
|
|
* |
193
|
|
|
* @param bool $fixed |
194
|
|
|
* |
195
|
|
|
* @return Field |
196
|
|
|
*/ |
197
|
1 |
|
public function fixed($fixed) |
198
|
|
|
{ |
199
|
1 |
|
$this->fieldBuilder->option('fixed', $fixed); |
200
|
|
|
|
201
|
1 |
|
return $this; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* The comment of the column in the schema (might not be supported by all vendors). |
206
|
|
|
* |
207
|
|
|
* @param string $comment |
208
|
|
|
* |
209
|
|
|
* @return Field |
210
|
|
|
*/ |
211
|
1 |
|
public function comment($comment) |
212
|
|
|
{ |
213
|
1 |
|
$this->fieldBuilder->option('comment', $comment); |
214
|
|
|
|
215
|
1 |
|
return $this; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* The collation of the column (only supported by Drizzle, Mysql, PostgreSQL>=9.1, Sqlite and SQLServer). |
220
|
|
|
* |
221
|
|
|
* @param string $collation |
222
|
|
|
* |
223
|
|
|
* @return Field |
224
|
|
|
*/ |
225
|
1 |
|
public function collation($collation) |
226
|
|
|
{ |
227
|
1 |
|
$this->fieldBuilder->option('collation', $collation); |
228
|
|
|
|
229
|
1 |
|
return $this; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* @return Field |
234
|
|
|
*/ |
235
|
8 |
|
public function primary() |
236
|
|
|
{ |
237
|
8 |
|
$this->fieldBuilder->makePrimaryKey(); |
238
|
|
|
|
239
|
8 |
|
return $this; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* @param null $name |
244
|
|
|
* |
245
|
|
|
* @return Field |
246
|
|
|
*/ |
247
|
2 |
|
public function index($name = null) |
248
|
|
|
{ |
249
|
2 |
|
$index = new Index( |
250
|
2 |
|
$this->metaDatabuilder, |
251
|
2 |
|
[$this->getName()] |
252
|
2 |
|
); |
253
|
|
|
|
254
|
2 |
|
if ($name !== null) { |
255
|
1 |
|
$index->name($name); |
256
|
1 |
|
} |
257
|
|
|
|
258
|
2 |
|
$this->callbackAndQueue($index); |
259
|
|
|
|
260
|
2 |
|
return $this; |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* @return Field |
265
|
|
|
*/ |
266
|
21 |
|
public function useForVersioning() |
267
|
|
|
{ |
268
|
21 |
|
$this->fieldBuilder->isVersionField(); |
269
|
|
|
|
270
|
21 |
|
return $this; |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* @return Field |
275
|
|
|
*/ |
276
|
191 |
|
public function build() |
277
|
|
|
{ |
278
|
191 |
|
$this->fieldBuilder->build(); |
279
|
|
|
|
280
|
175 |
|
$this->buildQueued(); |
281
|
|
|
|
282
|
174 |
|
return $this; |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* @return FieldBuilder |
287
|
|
|
*/ |
288
|
69 |
|
public function getBuilder() |
289
|
|
|
{ |
290
|
69 |
|
return $this->fieldBuilder; |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
/** |
294
|
|
|
* Magic call method works as a proxy for the Doctrine FieldBuilder. |
295
|
|
|
* |
296
|
|
|
* @param string $method |
297
|
|
|
* @param array $args |
298
|
|
|
* |
299
|
|
|
* @throws BadMethodCallException |
300
|
|
|
* @return $this |
301
|
|
|
*/ |
302
|
164 |
|
public function __call($method, $args) |
303
|
|
|
{ |
304
|
|
|
// Work around reserved keywords |
305
|
164 |
|
if ($method === 'default') { |
306
|
1 |
|
return call_user_func_array([$this, 'setDefault'], $args); |
307
|
|
|
} |
308
|
|
|
|
309
|
163 |
|
if ($this->hasMacro($method)) { |
310
|
108 |
|
return $this->queueMacro($method, $args); |
311
|
|
|
} |
312
|
|
|
|
313
|
62 |
|
if (method_exists($this->getBuilder(), $method)) { |
314
|
61 |
|
call_user_func_array([$this->getBuilder(), $method], $args); |
315
|
|
|
|
316
|
61 |
|
return $this; |
317
|
|
|
} |
318
|
|
|
|
319
|
1 |
|
throw new BadMethodCallException("FieldBuilder method [{$method}] does not exist."); |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
/** |
323
|
|
|
* The default value to set for the column if no value is supplied. |
324
|
|
|
* |
325
|
|
|
* @param string $default |
326
|
|
|
* |
327
|
|
|
* @return Field |
328
|
|
|
*/ |
329
|
1 |
|
protected function setDefault($default) |
330
|
|
|
{ |
331
|
1 |
|
$this->fieldBuilder->option('default', $default); |
332
|
|
|
|
333
|
1 |
|
return $this; |
334
|
|
|
} |
335
|
|
|
} |
336
|
|
|
|