1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LaravelDoctrine\Fluent\Builders; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\Mapping\Builder\FieldBuilder; |
6
|
|
|
use Doctrine\ORM\Mapping\ClassMetadataInfo; |
7
|
|
|
use LaravelDoctrine\Fluent\Buildable; |
8
|
|
|
|
9
|
|
|
class GeneratedValue implements Buildable |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var FieldBuilder |
13
|
|
|
*/ |
14
|
|
|
protected $builder; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
protected $strategy = 'AUTO'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
protected $name; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var int |
28
|
|
|
*/ |
29
|
|
|
protected $initial = 1; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var int |
33
|
|
|
*/ |
34
|
|
|
protected $size = 10; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
protected $generator; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var ClassMetadataInfo |
43
|
|
|
*/ |
44
|
|
|
protected $classMetadata; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param FieldBuilder $builder |
48
|
|
|
* @param ClassMetadataInfo $classMetadata |
49
|
|
|
*/ |
50
|
26 |
|
public function __construct(FieldBuilder $builder, ClassMetadataInfo $classMetadata) |
51
|
|
|
{ |
52
|
26 |
|
$this->builder = $builder; |
53
|
26 |
|
$this->classMetadata = $classMetadata; |
54
|
26 |
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Tells Doctrine to pick the strategy that is preferred by the used database platform. The preferred strategies |
58
|
|
|
* are IDENTITY for MySQL, SQLite, MsSQL and SQL Anywhere and SEQUENCE for Oracle and PostgreSQL. This strategy |
59
|
|
|
* provides full portability. |
60
|
|
|
* |
61
|
|
|
* @param string|null $name |
62
|
|
|
* @param string|null $initial |
63
|
|
|
* @param string|null $size |
64
|
|
|
* |
65
|
|
|
* @return $this |
66
|
|
|
*/ |
67
|
6 |
View Code Duplication |
public function auto($name = null, $initial = null, $size = null) |
68
|
|
|
{ |
69
|
6 |
|
$this->strategy = 'AUTO'; |
70
|
6 |
|
$this->customize($name, $initial, $size); |
71
|
|
|
|
72
|
6 |
|
return $this; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Tells Doctrine to use a database sequence for ID generation. This strategy does currently not provide full |
77
|
|
|
* portability. Sequences are supported by Oracle, PostgreSql and SQL Anywhere. |
78
|
|
|
* |
79
|
|
|
* @param string|null $name |
80
|
|
|
* @param string|null $initial |
81
|
|
|
* @param string|null $size |
82
|
|
|
* |
83
|
|
|
* @return $this |
84
|
|
|
*/ |
85
|
6 |
View Code Duplication |
public function sequence($name = null, $initial = null, $size = null) |
86
|
|
|
{ |
87
|
6 |
|
$this->strategy = 'SEQUENCE'; |
88
|
6 |
|
$this->customize($name, $initial, $size); |
89
|
|
|
|
90
|
6 |
|
return $this; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Tells Doctrine to use special identity columns in the database that generate a value on insertion of a row. |
95
|
|
|
* This strategy does currently not provide full portability and is supported by the following platforms: |
96
|
|
|
* MySQL/SQLite/SQL Anywhere (AUTO_INCREMENT), MSSQL (IDENTITY) and PostgreSQL (SERIAL). |
97
|
|
|
* |
98
|
|
|
* @return $this |
99
|
|
|
*/ |
100
|
2 |
|
public function identity() |
101
|
|
|
{ |
102
|
2 |
|
$this->strategy = 'IDENTITY'; |
103
|
|
|
|
104
|
2 |
|
return $this; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Tells Doctrine to use the built-in Universally Unique Identifier generator. |
109
|
|
|
* This strategy provides full portability. |
110
|
|
|
* |
111
|
|
|
* @return $this |
112
|
|
|
*/ |
113
|
1 |
|
public function uuid() |
114
|
|
|
{ |
115
|
1 |
|
$this->strategy = 'UUID'; |
116
|
|
|
|
117
|
1 |
|
return $this; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Tells Doctrine that the identifiers are assigned (and thus generated) by your code. The assignment must take |
122
|
|
|
* place before a new entity is passed to EntityManager#persist. |
123
|
|
|
* NONE is the same as leaving off the @GeneratedValue entirely. |
124
|
|
|
* |
125
|
|
|
* @return $this |
126
|
|
|
*/ |
127
|
1 |
|
public function none() |
128
|
|
|
{ |
129
|
1 |
|
$this->strategy = 'NONE'; |
130
|
|
|
|
131
|
1 |
|
return $this; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Tells Doctrine to use a custom Generator class to generate identifiers. |
136
|
|
|
* The given class must extend \Doctrine\ORM\Id\AbstractIdGenerator |
137
|
|
|
* |
138
|
|
|
* @param string $generatorClass |
139
|
|
|
* |
140
|
|
|
* @return $this |
141
|
|
|
*/ |
142
|
1 |
|
public function custom($generatorClass) |
143
|
|
|
{ |
144
|
1 |
|
$this->strategy = 'CUSTOM'; |
145
|
1 |
|
$this->generator = $generatorClass; |
146
|
|
|
|
147
|
1 |
|
return $this; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @param string|null $name |
152
|
|
|
* @param string|null $initial |
153
|
|
|
* @param string|null $size |
154
|
|
|
*/ |
155
|
12 |
|
private function customize($name, $initial, $size) |
156
|
|
|
{ |
157
|
12 |
|
$this->name = $name ?: $this->name; |
158
|
12 |
|
$this->initial = $initial ?: $this->initial; |
159
|
12 |
|
$this->size = $size ?: $this->size; |
160
|
12 |
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Execute the build process |
164
|
|
|
*/ |
165
|
26 |
|
public function build() |
166
|
|
|
{ |
167
|
26 |
|
$this->builder->generatedValue($this->strategy); |
168
|
|
|
|
169
|
26 |
|
if ($this->name) { |
170
|
9 |
|
$this->builder->setSequenceGenerator($this->name, $this->size, $this->initial); |
171
|
9 |
|
} |
172
|
|
|
|
173
|
26 |
|
if ($this->generator) { |
174
|
1 |
|
$this->classMetadata->setCustomGeneratorDefinition(['class' => $this->generator]); |
175
|
1 |
|
} |
176
|
26 |
|
} |
177
|
|
|
} |
178
|
|
|
|