1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Cycle ORM Schema Builder. |
5
|
|
|
* |
6
|
|
|
* @license MIT |
7
|
|
|
* @author Anton Titov (Wolfy-J) |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace Cycle\Schema\Relation\Traits; |
13
|
|
|
|
14
|
|
|
use Cycle\Schema\Definition\Entity; |
15
|
|
|
use Cycle\Schema\Definition\Field; |
16
|
|
|
use Cycle\Schema\Registry; |
17
|
|
|
use Cycle\Schema\Relation\OptionSchema; |
18
|
|
|
use Cycle\Schema\Relation\RelationSchema; |
19
|
|
|
|
20
|
|
|
trait ForeignKeyTrait |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Create foreign key between two entities. Only when both entities are located |
24
|
|
|
* in a same database. |
25
|
|
|
* |
26
|
|
|
* @param Registry $registry |
27
|
|
|
* @param Entity $source |
28
|
|
|
* @param Entity $target |
29
|
|
|
* @param Field $innerField |
30
|
|
|
* @param Field $outerField |
31
|
|
|
*/ |
32
|
|
|
protected function createForeignKey( |
33
|
|
|
Registry $registry, |
34
|
|
|
Entity $source, |
35
|
|
|
Entity $target, |
36
|
|
|
Field $innerField, |
37
|
|
|
Field $outerField |
38
|
|
|
): void { |
39
|
|
|
if ($registry->getDatabase($source) !== $registry->getDatabase($target)) { |
40
|
|
|
return; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$registry->getTableSchema($target) |
44
|
|
|
->foreignKey([$outerField->getColumn()]) |
45
|
|
|
->references($registry->getTable($source), [$innerField->getColumn()]) |
46
|
|
|
->onUpdate($this->getOptions()->get(RelationSchema::FK_ACTION)) |
|
|
|
|
47
|
|
|
->onDelete($this->getOptions()->get(RelationSchema::FK_ACTION)); |
|
|
|
|
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Create foreign key between two entities. Only when both entities are located |
53
|
|
|
* in a same database. |
54
|
|
|
* |
55
|
|
|
* @param Registry $registry |
56
|
|
|
* @param Entity $source |
57
|
|
|
* @param Entity $target |
58
|
|
|
* @param Field[] $innerFields |
59
|
|
|
* @param Field[] $outerFields |
60
|
|
|
*/ |
61
|
|
|
protected function createForeignCompositeKey( |
62
|
|
|
Registry $registry, |
63
|
|
|
Entity $source, |
64
|
|
|
Entity $target, |
65
|
|
|
array $innerFields, |
66
|
|
|
array $outerFields |
67
|
|
|
): void { |
68
|
|
|
if ($registry->getDatabase($source) !== $registry->getDatabase($target)) { |
69
|
|
|
return; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$registry->getTableSchema($target) |
73
|
|
|
->foreignKey(array_map(function (Field $field) { |
74
|
|
|
return $field->getColumn(); |
75
|
|
|
}, $outerFields)) |
76
|
|
|
->references($registry->getTable($source), array_map(function (Field $field) { |
77
|
|
|
return $field->getColumn(); |
78
|
|
|
}, $innerFields)) |
79
|
|
|
->onUpdate($this->getOptions()->get(RelationSchema::FK_ACTION)) |
|
|
|
|
80
|
|
|
->onDelete($this->getOptions()->get(RelationSchema::FK_ACTION)); |
|
|
|
|
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return OptionSchema |
85
|
|
|
*/ |
86
|
|
|
abstract protected function getOptions(): OptionSchema; |
87
|
|
|
} |
88
|
|
|
|