This repository was archived by the owner on Jan 24, 2024. It is now read-only.
File tree 5 files changed +47
-11
lines changed
5 files changed +47
-11
lines changed Original file line number Diff line number Diff line change @@ -145,8 +145,8 @@ abstract class RecordEntity extends AbstractRecord implements RecordInterface
145
145
*
146
146
* @see Record::INDEXES
147
147
*/
148
- const INDEX = 1000 ; //Default index type
149
- const UNIQUE = 2000 ; //Unique index definition
148
+ const INDEX = 1000 ; //Default index type
149
+ const UNIQUE = 2000 ; //Unique index definition
150
150
151
151
/*
152
152
* ================================================
Original file line number Diff line number Diff line change 12
12
*/
13
13
final class IndexDefinition
14
14
{
15
+ /**
16
+ * @var string|null
17
+ */
18
+ private $ name ;
19
+
15
20
/**
16
21
* @var array
17
22
*/
@@ -23,13 +28,16 @@ final class IndexDefinition
23
28
private $ unique ;
24
29
25
30
/**
26
- * @param array $columns
27
- * @param bool $unique
31
+ * @param array $columns
32
+ * @param bool $unique
33
+ * @param string|null $name
28
34
*/
29
- public function __construct (array $ columns , bool $ unique = false )
35
+ public function __construct (array $ columns , bool $ unique = false , string $ name = null )
30
36
{
31
37
$ this ->index = $ columns ;
32
38
$ this ->unique = $ unique ;
39
+
40
+ $ this ->name = $ name ;
33
41
}
34
42
35
43
/**
@@ -55,6 +63,10 @@ public function isUnique(): bool
55
63
*/
56
64
public function getName (): string
57
65
{
66
+ if (!empty ($ this ->name )) {
67
+ return $ this ->name ;
68
+ }
69
+
58
70
$ name = ($ this ->isUnique () ? 'unique_ ' : 'index_ ' ) . join ('_ ' , $ this ->getColumns ());
59
71
60
72
return strlen ($ name ) > 64 ? md5 ($ name ) : $ name ;
Original file line number Diff line number Diff line change @@ -330,16 +330,32 @@ protected function isRelation($type): bool
330
330
*/
331
331
protected function castIndex (array $ definition )
332
332
{
333
+ $ name = null ;
333
334
$ unique = null ;
334
335
$ columns = [];
335
336
336
- foreach ($ definition as $ chunk ) {
337
- if ($ chunk == RecordEntity::INDEX || $ chunk == RecordEntity::UNIQUE ) {
338
- $ unique = $ chunk === RecordEntity::UNIQUE ;
337
+
338
+ foreach ($ definition as $ key => $ value ) {
339
+
340
+ if ($ key == RecordEntity::INDEX || $ key == RecordEntity::UNIQUE ) {
341
+ $ unique = ($ key === RecordEntity::UNIQUE );
342
+
343
+ if (!is_string ($ value ) || empty ($ value )){
344
+ throw new DefinitionException (
345
+ "Record ' {$ this }' has index definition with invalid index name "
346
+ );
347
+ }
348
+
349
+ $ name = $ value ;
350
+ continue ;
351
+ }
352
+
353
+ if ($ value == RecordEntity::INDEX || $ value == RecordEntity::UNIQUE ) {
354
+ $ unique = ($ value === RecordEntity::UNIQUE );
339
355
continue ;
340
356
}
341
357
342
- $ columns [] = $ chunk ;
358
+ $ columns [] = $ value ;
343
359
}
344
360
345
361
if (is_null ($ unique )) {
@@ -354,7 +370,7 @@ protected function castIndex(array $definition)
354
370
);
355
371
}
356
372
357
- return new IndexDefinition ($ columns , $ unique );
373
+ return new IndexDefinition ($ columns , $ unique, $ name );
358
374
}
359
375
360
376
/**
Original file line number Diff line number Diff line change 7
7
namespace Spiral \Tests \ORM ;
8
8
9
9
use Spiral \ORM \Helpers \ColumnRenderer ;
10
+ use Spiral \Tests \ORM \Fixtures \User ;
10
11
11
12
abstract class ColumnRendererTest extends BaseTest
12
13
{
14
+ public function testNamedIndexes ()
15
+ {
16
+ $ table = $ this ->orm ->table (User::class);
17
+ $ this ->assertSame ('status_index ' , $ table ->getSchema ()->index (['status ' ])->getName ());
18
+ }
19
+
20
+
13
21
public function testRenderString ()
14
22
{
15
23
$ table = $ this ->db ->sample ->getSchema ();
Original file line number Diff line number Diff line change @@ -50,6 +50,6 @@ class User extends BaseRecord implements
50
50
];
51
51
52
52
const INDEXES = [
53
- [self ::INDEX , 'status ' ]
53
+ [self ::INDEX => ' status_index ' , 'status ' ]
54
54
];
55
55
}
You can’t perform that action at this time.
0 commit comments