1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Phinx |
4
|
|
|
* |
5
|
|
|
* (The MIT license) |
6
|
|
|
* Copyright (c) 2015 Rob Morgan |
7
|
|
|
* |
8
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
9
|
|
|
* of this software and associated * documentation files (the "Software"), to |
10
|
|
|
* deal in the Software without restriction, including without limitation the |
11
|
|
|
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
12
|
|
|
* sell copies of the Software, and to permit persons to whom the Software is |
13
|
|
|
* furnished to do so, subject to the following conditions: |
14
|
|
|
* |
15
|
|
|
* The above copyright notice and this permission notice shall be included in |
16
|
|
|
* all copies or substantial portions of the Software. |
17
|
|
|
* |
18
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
19
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
20
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
21
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
22
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
23
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
24
|
|
|
* IN THE SOFTWARE. |
25
|
|
|
* |
26
|
|
|
* @package Phinx |
27
|
|
|
* @subpackage Phinx\Db |
28
|
|
|
*/ |
29
|
|
|
namespace Phinx\Db; |
30
|
|
|
|
31
|
|
|
use Phinx\Db\Action\AddColumn; |
32
|
|
|
use Phinx\Db\Action\AddForeignKey; |
33
|
|
|
use Phinx\Db\Action\AddIndex; |
34
|
|
|
use Phinx\Db\Action\ChangeColumn; |
35
|
|
|
use Phinx\Db\Action\CreateTable; |
36
|
|
|
use Phinx\Db\Action\DropColumn; |
37
|
|
|
use Phinx\Db\Action\DropForeignKey; |
38
|
|
|
use Phinx\Db\Action\DropIndex; |
39
|
|
|
use Phinx\Db\Action\DropTable; |
40
|
|
|
use Phinx\Db\Action\RemoveColumn; |
41
|
|
|
use Phinx\Db\Action\RenameColumn; |
42
|
|
|
use Phinx\Db\Action\RenameTable; |
43
|
|
|
use Phinx\Db\Adapter\AdapterInterface; |
44
|
|
|
use Phinx\Db\Plan\Intent; |
45
|
|
|
use Phinx\Db\Plan\Plan; |
46
|
|
|
use Phinx\Db\Table\Column; |
47
|
|
|
use Phinx\Db\Table\ForeignKey; |
48
|
|
|
use Phinx\Db\Table\Index; |
49
|
|
|
use Phinx\Db\Table\Table as TableValue; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* |
53
|
|
|
* This object is based loosely on: http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/Table.html. |
54
|
|
|
*/ |
55
|
|
|
class Table |
56
|
|
|
{ |
57
|
|
|
/** |
58
|
|
|
* @var \Phinx\Db\Table\Table |
59
|
|
|
*/ |
60
|
|
|
protected $table; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @var \Phinx\Db\Adapter\AdapterInterface |
64
|
|
|
*/ |
65
|
|
|
protected $adapter; |
66
|
|
|
|
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @var \Phinx\Db\Plan\Intent |
70
|
|
|
*/ |
71
|
|
|
protected $actions; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @var array |
75
|
|
|
*/ |
76
|
|
|
protected $data = []; |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Class Constuctor. |
80
|
|
|
* |
81
|
|
|
* @param string $name Table Name |
82
|
|
|
* @param array $options Options |
83
|
|
|
* @param \Phinx\Db\Adapter\AdapterInterface $adapter Database Adapter |
84
|
239 |
|
*/ |
85
|
|
|
public function __construct($name, $options = [], AdapterInterface $adapter = null) |
86
|
239 |
|
{ |
87
|
239 |
|
$this->table = new TableValue($name, $options); |
88
|
|
|
$this->actions = new Intent(); |
89
|
239 |
|
|
90
|
231 |
|
if ($adapter !== null) { |
91
|
231 |
|
$this->setAdapter($adapter); |
92
|
239 |
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Gets the table name. |
97
|
|
|
* |
98
|
|
|
* @return string|null |
99
|
|
|
*/ |
100
|
239 |
|
public function getName() |
101
|
|
|
{ |
102
|
239 |
|
return $this->table->getName(); |
103
|
239 |
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Gets the table options. |
107
|
|
|
* |
108
|
|
|
* @return array |
109
|
|
|
*/ |
110
|
|
|
public function getOptions() |
111
|
215 |
|
{ |
112
|
|
|
return $this->table->getOptions(); |
113
|
215 |
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Gets the table name and options as an object |
117
|
|
|
* |
118
|
|
|
* @return \Phinx\Db\Table\Table |
119
|
|
|
*/ |
120
|
|
|
public function getTable() |
121
|
|
|
{ |
122
|
239 |
|
return $this->table; |
123
|
|
|
} |
124
|
239 |
|
|
125
|
239 |
|
/** |
126
|
|
|
* Sets the database adapter. |
127
|
|
|
* |
128
|
|
|
* @param \Phinx\Db\Adapter\AdapterInterface $adapter Database Adapter |
129
|
|
|
* @return \Phinx\Db\Table |
130
|
|
|
*/ |
131
|
|
|
public function setAdapter(AdapterInterface $adapter) |
132
|
|
|
{ |
133
|
189 |
|
$this->adapter = $adapter; |
134
|
|
|
|
135
|
189 |
|
return $this; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Gets the database adapter. |
140
|
|
|
* |
141
|
|
|
* @return \Phinx\Db\Adapter\AdapterInterface|null |
142
|
|
|
*/ |
143
|
|
|
public function getAdapter() |
144
|
231 |
|
{ |
145
|
|
|
if (!$this->adapter) { |
146
|
231 |
|
throw new \RuntimeException('There is no database adapter set yet, cannot proceed'); |
147
|
231 |
|
} |
148
|
|
|
|
149
|
|
|
return $this->adapter; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Does the table exist? |
154
|
|
|
* |
155
|
225 |
|
* @return bool |
156
|
|
|
*/ |
157
|
225 |
|
public function exists() |
158
|
|
|
{ |
159
|
|
|
return $this->getAdapter()->hasTable($this->getName()); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Drops the database table. |
164
|
|
|
* |
165
|
195 |
|
* @return \Phinx\Db\Table |
166
|
|
|
*/ |
167
|
195 |
|
public function drop() |
168
|
|
|
{ |
169
|
|
|
$this->actions->addAction(new DropTable($this->table)); |
170
|
|
|
|
171
|
|
|
return $this; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
1 |
|
* Renames the database table. |
176
|
|
|
* |
177
|
1 |
|
* @param string $newTableName New Table Name |
178
|
1 |
|
* @return \Phinx\Db\Table |
179
|
|
|
*/ |
180
|
|
|
public function rename($newTableName) |
181
|
|
|
{ |
182
|
|
|
$this->actions->addAction(new RenameTable($this->table, $newTableName)); |
183
|
|
|
|
184
|
|
|
return $this; |
185
|
|
|
} |
186
|
3 |
|
|
187
|
|
|
/** |
188
|
3 |
|
* Gets an array of the table columns. |
189
|
3 |
|
* |
190
|
3 |
|
* @return \Phinx\Db\Table\Column[] |
191
|
|
|
*/ |
192
|
|
|
public function getColumns() |
193
|
|
|
{ |
194
|
|
|
return $this->getAdapter()->getColumns($this->getName()); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Sets an array of data to be inserted. |
199
|
|
|
* |
200
|
|
|
* @param array $data Data |
201
|
|
|
* @return \Phinx\Db\Table |
202
|
|
|
*/ |
203
|
|
|
public function setData($data) |
204
|
|
|
{ |
205
|
|
|
$this->data = $data; |
206
|
|
|
|
207
|
|
|
return $this; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
10 |
|
* Gets the data waiting to be inserted. |
212
|
|
|
* |
213
|
10 |
|
* @return array |
214
|
|
|
*/ |
215
|
|
|
public function getData() |
216
|
|
|
{ |
217
|
|
|
return $this->data; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Resets all of the pending table changes. |
222
|
196 |
|
* |
223
|
|
|
* @return void |
224
|
196 |
|
*/ |
225
|
196 |
|
public function reset() |
226
|
|
|
{ |
227
|
|
|
$this->actions = new Intent(); |
228
|
|
|
$this->setData([]); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* Add a table column. |
233
|
204 |
|
* |
234
|
|
|
* Type can be: string, text, integer, float, decimal, datetime, timestamp, |
235
|
204 |
|
* time, date, binary, boolean. |
236
|
|
|
* |
237
|
|
|
* Valid options can be: limit, default, null, precision or scale. |
238
|
|
|
* |
239
|
|
|
* @param string|\Phinx\Db\Table\Column $columnName Column Name |
240
|
|
|
* @param string|\Phinx\Util\Literal $type Column Type |
241
|
|
|
* @param array $options Column Options |
242
|
|
|
* @throws \RuntimeException |
243
|
|
|
* @throws \InvalidArgumentException |
244
|
196 |
|
* @return \Phinx\Db\Table |
245
|
|
|
*/ |
246
|
196 |
|
public function addColumn($columnName, $type = null, $options = []) |
247
|
196 |
|
{ |
248
|
|
View Code Duplication |
if ($columnName instanceof Column) { |
|
|
|
|
249
|
|
|
$action = new AddColumn($this->table, $columnName); |
250
|
|
|
} else { |
251
|
|
|
$action = AddColumn::build($this->table, $columnName, $type, $options); |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
// Delegate to Adapters to check column type |
255
|
191 |
|
if (!$this->getAdapter()->isValidColumnType($action->getColumn())) { |
256
|
|
|
throw new \InvalidArgumentException(sprintf( |
257
|
191 |
|
'An invalid column type "%s" was specified for column "%s".', |
258
|
|
|
$type, |
259
|
|
|
$action->getColumn()->getName() |
260
|
|
|
)); |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
$this->actions->addAction($action); |
264
|
|
|
|
265
|
|
|
return $this; |
266
|
196 |
|
} |
267
|
|
|
|
268
|
196 |
|
/** |
269
|
196 |
|
* Remove a table column. |
270
|
|
|
* |
271
|
|
|
* @param string $columnName Column Name |
272
|
|
|
* @return \Phinx\Db\Table |
273
|
|
|
*/ |
274
|
|
|
public function removeColumn($columnName) |
275
|
|
|
{ |
276
|
|
|
$action = RemoveColumn::build($this->table, $columnName); |
277
|
192 |
|
$this->actions->addAction($action); |
278
|
|
|
|
279
|
192 |
|
return $this; |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* Rename a table column. |
284
|
|
|
* |
285
|
|
|
* @param string $oldName Old Column Name |
286
|
|
|
* @param string $newName New Column Name |
287
|
|
|
* @return \Phinx\Db\Table |
288
|
196 |
|
*/ |
289
|
|
|
public function renameColumn($oldName, $newName) |
290
|
196 |
|
{ |
291
|
196 |
|
$action = RenameColumn::build($this->table, $oldName, $newName); |
292
|
|
|
$this->actions->addAction($action); |
293
|
|
|
|
294
|
|
|
return $this; |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* Change a table column type. |
299
|
197 |
|
* |
300
|
|
|
* @param string $columnName Column Name |
301
|
197 |
|
* @param string|\Phinx\Db\Table\Column|\Phinx\Util\Literal $newColumnType New Column Type |
302
|
|
|
* @param array $options Options |
303
|
|
|
* @return \Phinx\Db\Table |
304
|
|
|
*/ |
305
|
|
|
public function changeColumn($columnName, $newColumnType, array $options = []) |
306
|
|
|
{ |
307
|
|
View Code Duplication |
if ($newColumnType instanceof Column) { |
|
|
|
|
308
|
|
|
$action = new ChangeColumn($this->table, $columnName, $newColumnType); |
309
|
196 |
|
} else { |
310
|
|
|
$action = ChangeColumn::build($this->table, $columnName, $newColumnType, $options); |
311
|
196 |
|
} |
312
|
196 |
|
$this->actions->addAction($action); |
313
|
196 |
|
|
314
|
196 |
|
return $this; |
315
|
196 |
|
} |
316
|
|
|
|
317
|
|
|
/** |
318
|
|
|
* Checks to see if a column exists. |
319
|
|
|
* |
320
|
|
|
* @param string $columnName Column Name |
321
|
|
|
* @return bool |
322
|
|
|
*/ |
323
|
|
|
public function hasColumn($columnName) |
324
|
|
|
{ |
325
|
|
|
return $this->getAdapter()->hasColumn($this->getName(), $columnName); |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
/** |
329
|
|
|
* Add an index to a database table. |
330
|
|
|
* |
331
|
|
|
* In $options you can specific unique = true/false or name (index name). |
332
|
210 |
|
* |
333
|
|
|
* @param string|array|\Phinx\Db\Table\Index $columns Table Column(s) |
334
|
|
|
* @param array $options Index Options |
335
|
210 |
|
* @return \Phinx\Db\Table |
336
|
1 |
|
*/ |
337
|
|
|
public function addIndex($columns, array $options = []) |
338
|
|
|
{ |
339
|
|
|
$action = AddIndex::build($this->table, $columns, $options); |
340
|
209 |
|
$this->actions->addAction($action); |
341
|
207 |
|
|
342
|
207 |
|
return $this; |
343
|
207 |
|
} |
344
|
207 |
|
|
345
|
207 |
|
/** |
346
|
2 |
|
* Removes the given index from a table. |
347
|
|
|
* |
348
|
|
|
* @param array $columns Columns |
349
|
|
|
* @return \Phinx\Db\Table |
350
|
209 |
|
*/ |
351
|
1 |
|
public function removeIndex(array $columns) |
352
|
1 |
|
{ |
353
|
1 |
|
$action = DropIndex::build($this->table, $columns); |
354
|
1 |
|
$this->actions->addAction($action); |
355
|
1 |
|
|
356
|
|
|
return $this; |
357
|
|
|
} |
358
|
208 |
|
|
359
|
208 |
|
/** |
360
|
|
|
* Removes the given index identified by its name from a table. |
361
|
|
|
* |
362
|
|
|
* @param string $name Index name |
363
|
|
|
* @return \Phinx\Db\Table |
364
|
|
|
*/ |
365
|
|
|
public function removeIndexByName($name) |
366
|
|
|
{ |
367
|
|
|
$action = DropIndex::buildFromName($this->table, $name); |
368
|
1 |
|
$this->actions->addAction($action); |
369
|
|
|
|
370
|
1 |
|
return $this; |
371
|
1 |
|
} |
372
|
|
|
|
373
|
|
|
/** |
374
|
|
|
* Checks to see if an index exists. |
375
|
|
|
* |
376
|
|
|
* @param string|array $columns Columns |
377
|
|
|
* @return bool |
378
|
|
|
*/ |
379
|
|
|
public function hasIndex($columns) |
380
|
|
|
{ |
381
|
4 |
|
return $this->getAdapter()->hasIndex($this->getName(), $columns); |
382
|
|
|
} |
383
|
4 |
|
|
384
|
4 |
|
/** |
385
|
|
|
* Checks to see if an index specified by name exists. |
386
|
|
|
* |
387
|
|
|
* @param string $indexName |
388
|
|
|
* @return bool |
389
|
|
|
*/ |
390
|
|
|
public function hasIndexByName($indexName) |
391
|
|
|
{ |
392
|
|
|
return $this->getAdapter()->hasIndexByName($this->getName(), $indexName); |
393
|
|
|
} |
394
|
|
|
|
395
|
17 |
|
/** |
396
|
|
|
* Add a foreign key to a database table. |
397
|
|
|
* |
398
|
17 |
|
* In $options you can specify on_delete|on_delete = cascade|no_action .., |
399
|
4 |
|
* on_update, constraint = constraint name. |
400
|
4 |
|
* |
401
|
4 |
|
* @param string|array $columns Columns |
402
|
4 |
|
* @param string|\Phinx\Db\Table $referencedTable Referenced Table |
403
|
13 |
|
* @param string|array $referencedColumns Referenced Columns |
404
|
|
|
* @param array $options Options |
405
|
|
|
* @return \Phinx\Db\Table |
406
|
|
|
*/ |
407
|
17 |
View Code Duplication |
public function addForeignKey($columns, $referencedTable, $referencedColumns = ['id'], $options = []) |
|
|
|
|
408
|
15 |
|
{ |
409
|
15 |
|
$action = AddForeignKey::build($this->table, $columns, $referencedTable, $referencedColumns); |
|
|
|
|
410
|
|
|
$this->actions->addAction($action); |
411
|
17 |
|
|
412
|
17 |
|
return $this; |
413
|
|
|
} |
414
|
|
|
|
415
|
|
|
/** |
416
|
|
|
* Add a foreign key to a database table with a given name. |
417
|
|
|
* |
418
|
|
|
* In $options you can specify on_delete|on_delete = cascade|no_action .., |
419
|
|
|
* on_update, constraint = constraint name. |
420
|
|
|
* |
421
|
89 |
|
* @param string $name The constaint name |
422
|
|
|
* @param string|array $columns Columns |
423
|
89 |
|
* @param string|\Phinx\Db\Table $referencedTable Referenced Table |
424
|
|
|
* @param string|array $referencedColumns Referenced Columns |
425
|
|
|
* @param array $options Options |
426
|
|
|
* @return \Phinx\Db\Table |
427
|
|
|
*/ |
428
|
|
View Code Duplication |
public function addForeignKeyWithName($name, $columns, $referencedTable, $referencedColumns = ['id'], $options = []) |
|
|
|
|
429
|
|
|
{ |
430
|
|
|
$action = AddForeignKey::build( |
431
|
|
|
$this->table, |
432
|
|
|
$columns, |
|
|
|
|
433
|
|
|
$referencedTable, |
|
|
|
|
434
|
|
|
$referencedColumns, |
435
|
29 |
|
$options, |
436
|
|
|
$name |
437
|
|
|
); |
438
|
29 |
|
$this->actions->addAction($action); |
439
|
28 |
|
|
440
|
28 |
|
return $this; |
441
|
22 |
|
} |
442
|
22 |
|
|
443
|
28 |
|
/** |
444
|
28 |
|
* Removes the given foreign key from the table. |
445
|
28 |
|
* |
446
|
1 |
|
* @param string|array $columns Column(s) |
447
|
|
|
* @param null|string $constraint Constraint names |
448
|
|
|
* @return \Phinx\Db\Table |
449
|
29 |
|
*/ |
450
|
29 |
View Code Duplication |
public function dropForeignKey($columns, $constraint = null) |
|
|
|
|
451
|
|
|
{ |
452
|
|
|
$action = DropForeignKey::build($this->table, $columns, $constraint); |
|
|
|
|
453
|
|
|
$this->actions->addAction($action); |
454
|
|
|
|
455
|
|
|
return $this; |
456
|
|
|
} |
457
|
|
|
|
458
|
|
|
/** |
459
|
1 |
|
* Checks to see if a foreign key exists. |
460
|
|
|
* |
461
|
1 |
|
* @param string|array $columns Column(s) |
462
|
1 |
|
* @param null|string $constraint Constraint names |
463
|
|
|
* @return bool |
464
|
|
|
*/ |
465
|
|
|
public function hasForeignKey($columns, $constraint = null) |
466
|
|
|
{ |
467
|
|
|
return $this->getAdapter()->hasForeignKey($this->getName(), $columns, $constraint); |
|
|
|
|
468
|
|
|
} |
469
|
|
|
|
470
|
|
|
/** |
471
|
1 |
|
* Add timestamp columns created_at and updated_at to the table. |
472
|
|
|
* |
473
|
1 |
|
* @param string|null $createdAt Alternate name for the created_at column |
474
|
1 |
|
* @param string|null $updatedAt Alternate name for the updated_at column |
475
|
|
|
* @param bool $withTimezone Whether to set the timezone option on the added columns |
476
|
|
|
* |
477
|
|
|
* @return \Phinx\Db\Table |
478
|
|
|
*/ |
479
|
|
|
public function addTimestamps($createdAt = 'created_at', $updatedAt = 'updated_at', $withTimezone = false) |
480
|
|
|
{ |
481
|
|
|
$createdAt = is_null($createdAt) ? 'created_at' : $createdAt; |
482
|
|
|
$updatedAt = is_null($updatedAt) ? 'updated_at' : $updatedAt; |
483
|
|
|
|
484
|
12 |
|
$this->addColumn($createdAt, 'timestamp', [ |
485
|
|
|
'default' => 'CURRENT_TIMESTAMP', |
486
|
12 |
|
'update' => '', |
487
|
|
|
'timezone' => $withTimezone, |
488
|
|
|
]) |
489
|
|
|
->addColumn($updatedAt, 'timestamp', [ |
490
|
|
|
'null' => true, |
491
|
|
|
'default' => null, |
492
|
|
|
'timezone' => $withTimezone, |
493
|
|
|
]); |
494
|
|
|
|
495
|
|
|
return $this; |
496
|
|
|
} |
497
|
|
|
|
498
|
|
|
/** |
499
|
|
|
* Alias that always sets $withTimezone to true |
500
|
|
|
* @see addTimestamps |
501
|
8 |
|
* |
502
|
|
|
* @param string|null $createdAt Alternate name for the created_at column |
503
|
8 |
|
* @param string|null $updatedAt Alternate name for the updated_at column |
504
|
4 |
|
* |
505
|
4 |
|
* @return \Phinx\Db\Table |
506
|
8 |
|
*/ |
507
|
8 |
|
public function addTimestampsWithTimezone($createdAt = null, $updatedAt = null) |
508
|
|
|
{ |
509
|
|
|
$this->addTimestamps($createdAt, $updatedAt, true); |
510
|
8 |
|
|
511
|
|
|
return $this; |
512
|
8 |
|
} |
513
|
8 |
|
|
514
|
8 |
|
/** |
515
|
8 |
|
* Insert data into the table. |
516
|
|
|
* |
517
|
8 |
|
* @param array $data array of data in the form: |
518
|
|
|
* array( |
519
|
|
|
* array("col1" => "value1", "col2" => "anotherValue1"), |
520
|
|
|
* array("col2" => "value2", "col2" => "anotherValue2"), |
521
|
|
|
* ) |
522
|
|
|
* or array("col1" => "value1", "col2" => "anotherValue1") |
523
|
|
|
* |
524
|
|
|
* @return \Phinx\Db\Table |
525
|
|
|
*/ |
526
|
|
|
public function insert($data) |
527
|
1 |
|
{ |
528
|
|
|
// handle array of array situations |
529
|
1 |
|
if (isset($data[0]) && is_array($data[0])) { |
530
|
1 |
|
foreach ($data as $row) { |
531
|
1 |
|
$this->data[] = $row; |
532
|
1 |
|
} |
533
|
|
|
|
534
|
|
|
return $this; |
535
|
1 |
|
} |
536
|
|
|
$this->data[] = $data; |
537
|
|
|
|
538
|
1 |
|
return $this; |
539
|
|
|
} |
540
|
|
|
|
541
|
|
|
/** |
542
|
|
|
* Creates a table from the object instance. |
543
|
|
|
* |
544
|
|
|
* @return void |
545
|
|
|
*/ |
546
|
|
|
public function create() |
547
|
|
|
{ |
548
|
1 |
|
$this->executeActions(false); |
549
|
|
|
$this->saveData(); |
550
|
1 |
|
$this->reset(); // reset pending changes |
551
|
|
|
} |
552
|
|
|
|
553
|
|
|
/** |
554
|
|
|
* Updates a table from the object instance. |
555
|
|
|
* |
556
|
|
|
* @throws \RuntimeException |
557
|
|
|
* @return void |
558
|
|
|
*/ |
559
|
|
|
public function update() |
560
|
|
|
{ |
561
|
15 |
|
$this->executeActions(true); |
562
|
|
|
$this->saveData(); |
563
|
15 |
|
$this->reset(); // reset pending changes |
564
|
15 |
|
} |
565
|
15 |
|
|
566
|
15 |
|
/** |
567
|
|
|
* Commit the pending data waiting for insertion. |
568
|
15 |
|
* |
569
|
15 |
|
* @return void |
570
|
15 |
|
*/ |
571
|
|
|
public function saveData() |
572
|
15 |
|
{ |
573
|
|
|
$rows = $this->getData(); |
574
|
15 |
|
if (empty($rows)) { |
575
|
|
|
return; |
576
|
|
|
} |
577
|
|
|
|
578
|
|
|
$bulk = true; |
579
|
|
|
$row = current($rows); |
580
|
|
|
$c = array_keys($row); |
581
|
|
|
foreach ($this->getData() as $row) { |
582
|
|
|
$k = array_keys($row); |
583
|
|
|
if ($k != $c) { |
584
|
|
|
$bulk = false; |
585
|
|
|
break; |
586
|
|
|
} |
587
|
|
|
} |
588
|
|
|
|
589
|
17 |
|
if ($bulk) { |
590
|
|
|
$this->getAdapter()->bulkinsert($this->table, $this->getData()); |
591
|
|
|
} else { |
592
|
17 |
|
foreach ($this->getData() as $row) { |
593
|
11 |
|
$this->getAdapter()->insert($this->table, $row); |
594
|
11 |
|
} |
595
|
11 |
|
} |
596
|
11 |
|
} |
597
|
|
|
|
598
|
8 |
|
/** |
599
|
8 |
|
* Immediately truncates the table. This operation cannot be undone |
600
|
|
|
* |
601
|
|
|
* @return void |
602
|
|
|
*/ |
603
|
|
|
public function truncate() |
604
|
|
|
{ |
605
|
|
|
$this->getAdapter()->truncateTable($this->getName()); |
606
|
|
|
} |
607
|
196 |
|
|
608
|
|
|
/** |
609
|
196 |
|
* Commits the table changes. |
610
|
196 |
|
* |
611
|
196 |
|
* If the table doesn't exist it is created otherwise it is updated. |
612
|
196 |
|
* |
613
|
|
|
* @return void |
614
|
|
|
*/ |
615
|
|
|
public function save() |
616
|
|
|
{ |
617
|
|
|
if ($this->exists()) { |
618
|
|
|
$this->update(); // update the table |
619
|
|
|
} else { |
620
|
46 |
|
$this->create(); // create the table |
621
|
|
|
} |
622
|
46 |
|
|
623
|
|
|
$this->reset(); // reset pending changes |
624
|
|
|
} |
625
|
|
|
|
626
|
|
|
/** |
627
|
46 |
|
* Executes all the pending actions for this table |
628
|
38 |
|
* |
629
|
46 |
|
* @param bool $exists Whether or not the table existed prior to executing this method |
630
|
|
|
* @return void |
631
|
46 |
|
*/ |
632
|
6 |
|
protected function executeActions($exists) |
633
|
46 |
|
{ |
634
|
|
|
// Renaming a table is tricky, specially when running a reversible migration |
635
|
46 |
|
// down. We will just assume the table already exists if the user commands a |
636
|
3 |
|
// table rename. |
637
|
46 |
|
$renamed = collection($this->actions->getActions()) |
638
|
|
|
->filter(function ($action) { |
639
|
46 |
|
return $action instanceof RenameTable; |
640
|
46 |
|
}) |
641
|
46 |
|
->first(); |
642
|
|
|
|
643
|
|
|
if ($renamed) { |
644
|
|
|
$exists = true; |
645
|
|
|
} |
646
|
|
|
|
647
|
|
|
// If the table does not exist, the last command in the chain needs to be |
648
|
196 |
|
// a CreateTable action. |
649
|
|
|
if (!$exists) { |
650
|
196 |
|
$this->actions->addAction(new CreateTable($this->table)); |
651
|
196 |
|
} |
652
|
192 |
|
|
653
|
|
|
$plan = new Plan($this->actions); |
654
|
|
|
$plan->execute($this->getAdapter()); |
|
|
|
|
655
|
12 |
|
} |
656
|
|
|
} |
657
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.