|
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\Definition\Map\FieldMap; |
|
17
|
|
|
use Cycle\Schema\Registry; |
|
18
|
|
|
use Cycle\Schema\Relation\OptionSchema; |
|
19
|
|
|
use Cycle\Schema\Relation\RelationSchema; |
|
20
|
|
|
|
|
21
|
|
|
trait ForeignKeyTrait |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* Create foreign key between two entities. Only when both entities are located |
|
25
|
|
|
* in a same database. |
|
26
|
|
|
* |
|
27
|
|
|
* @param Registry $registry |
|
28
|
|
|
* @param Entity $source |
|
29
|
|
|
* @param Entity $target |
|
30
|
|
|
* @param Field $innerField |
|
31
|
|
|
* @param Field $outerField |
|
32
|
|
|
*/ |
|
33
|
|
|
protected function createForeignKey( |
|
34
|
|
|
Registry $registry, |
|
35
|
|
|
Entity $source, |
|
36
|
|
|
Entity $target, |
|
37
|
|
|
Field $innerField, |
|
38
|
|
|
Field $outerField |
|
39
|
|
|
): void { |
|
40
|
|
|
if ($registry->getDatabase($source) !== $registry->getDatabase($target)) { |
|
41
|
|
|
return; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
$outerFields = (new FieldMap())->set($outerField->getColumn(), $outerField); |
|
45
|
|
|
$innerFields = (new FieldMap())->set($innerField->getColumn(), $innerField); |
|
46
|
|
|
|
|
47
|
|
|
$this->createForeignCompositeKey($registry, $source, $target, $outerFields, $innerFields); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Create foreign key between two entities with composite fields. 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 FieldMap $innerFields |
|
59
|
|
|
* @param FieldMap $outerFields |
|
60
|
|
|
*/ |
|
61
|
|
|
protected function createForeignCompositeKey( |
|
62
|
|
|
Registry $registry, |
|
63
|
|
|
Entity $source, |
|
64
|
|
|
Entity $target, |
|
65
|
|
|
FieldMap $innerFields, |
|
66
|
|
|
FieldMap $outerFields |
|
67
|
|
|
): void { |
|
68
|
|
|
if ($registry->getDatabase($source) !== $registry->getDatabase($target)) { |
|
69
|
|
|
return; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
$registry->getTableSchema($target) |
|
73
|
|
|
->foreignKey($outerFields->getColumnNames()) |
|
74
|
|
|
->references($registry->getTable($source), $innerFields->getColumnNames()) |
|
75
|
|
|
->onUpdate($this->getOptions()->get(RelationSchema::FK_ACTION)) |
|
|
|
|
|
|
76
|
|
|
->onDelete($this->getOptions()->get(RelationSchema::FK_ACTION)); |
|
|
|
|
|
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @return OptionSchema |
|
81
|
|
|
*/ |
|
82
|
|
|
abstract protected function getOptions(): OptionSchema; |
|
83
|
|
|
} |
|
84
|
|
|
|