1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Cycle ORM Schema Builder. |
4
|
|
|
* |
5
|
|
|
* @license MIT |
6
|
|
|
* @author Anton Titov (Wolfy-J) |
7
|
|
|
*/ |
8
|
|
|
declare(strict_types=1); |
9
|
|
|
|
10
|
|
|
namespace Cycle\Schema\Relation; |
11
|
|
|
|
12
|
|
|
use Cycle\ORM\Relation; |
13
|
|
|
use Cycle\Schema\Exception\RelationException; |
14
|
|
|
use Cycle\Schema\InversableInterface; |
15
|
|
|
use Cycle\Schema\Registry; |
16
|
|
|
use Cycle\Schema\Relation\Traits\FieldTrait; |
17
|
|
|
use Cycle\Schema\Relation\Traits\ForeignKeyTrait; |
18
|
|
|
use Cycle\Schema\RelationInterface; |
19
|
|
|
|
20
|
|
|
class HasMany extends RelationSchema implements InversableInterface |
21
|
|
|
{ |
22
|
|
|
use FieldTrait, ForeignKeyTrait; |
23
|
|
|
|
24
|
|
|
// internal relation type |
25
|
|
|
protected const RELATION_TYPE = Relation::HAS_MANY; |
26
|
|
|
|
27
|
|
|
// relation schema options |
28
|
|
|
protected const RELATION_SCHEMA = [ |
29
|
|
|
// save with parent |
30
|
|
|
Relation::CASCADE => true, |
31
|
|
|
|
32
|
|
|
// do not pre-load relation by default |
33
|
|
|
Relation::LOAD => null, |
34
|
|
|
|
35
|
|
|
// custom where condition |
36
|
|
|
Relation::WHERE => [], |
37
|
|
|
|
38
|
|
|
// not nullable by default |
39
|
|
|
Relation::NULLABLE => false, |
40
|
|
|
|
41
|
|
|
// link to parent entity primary key by default |
42
|
|
|
Relation::INNER_KEY => '{source:primaryKey}', |
43
|
|
|
|
44
|
|
|
// default field name for inner key |
45
|
|
|
Relation::OUTER_KEY => '{source:role}_{innerKey}', |
46
|
|
|
|
47
|
|
|
// rendering options |
48
|
|
|
RelationSchema::INDEX_CREATE => true, |
49
|
|
|
RelationSchema::FK_CREATE => true, |
50
|
|
|
RelationSchema::FK_ACTION => 'CASCADE' |
51
|
|
|
]; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param Registry $registry |
55
|
|
|
*/ |
56
|
|
|
public function compute(Registry $registry) |
57
|
|
|
{ |
58
|
|
|
parent::compute($registry); |
59
|
|
|
|
60
|
|
|
$source = $registry->getEntity($this->source); |
61
|
|
|
$target = $registry->getEntity($this->target); |
62
|
|
|
|
63
|
|
|
// create target outer field |
64
|
|
|
$this->ensureField( |
65
|
|
|
$target, |
66
|
|
|
$this->options->get(Relation::OUTER_KEY), |
67
|
|
|
$this->getField($source, Relation::INNER_KEY), |
68
|
|
|
$this->options->get(Relation::NULLABLE) |
|
|
|
|
69
|
|
|
); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param Registry $registry |
74
|
|
|
*/ |
75
|
|
|
public function render(Registry $registry) |
76
|
|
|
{ |
77
|
|
|
$source = $registry->getEntity($this->source); |
78
|
|
|
$target = $registry->getEntity($this->target); |
79
|
|
|
|
80
|
|
|
$innerField = $this->getField($source, Relation::INNER_KEY); |
81
|
|
|
$outerField = $this->getField($target, Relation::OUTER_KEY); |
82
|
|
|
|
83
|
|
|
$table = $registry->getTableSchema($target); |
84
|
|
|
|
85
|
|
|
if ($this->options->get(self::INDEX_CREATE)) { |
86
|
|
|
$table->index([$outerField->getColumn()]); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
if ($this->options->get(self::FK_CREATE)) { |
90
|
|
|
$this->createForeignKey($registry, $source, $target, $innerField, $outerField); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param Registry $registry |
96
|
|
|
* @return array |
97
|
|
|
*/ |
98
|
|
|
public function inverseTargets(Registry $registry): array |
99
|
|
|
{ |
100
|
|
|
return [ |
101
|
|
|
$registry->getEntity($this->target) |
102
|
|
|
]; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param RelationInterface $relation |
107
|
|
|
* @param string $into |
108
|
|
|
* @param int|null $load |
109
|
|
|
* @return RelationInterface |
110
|
|
|
* |
111
|
|
|
* @throws RelationException |
112
|
|
|
*/ |
113
|
|
|
public function inverseRelation(RelationInterface $relation, string $into, ?int $load = null): RelationInterface |
114
|
|
|
{ |
115
|
|
|
if (!$relation instanceof BelongsTo && !$relation instanceof RefersTo) { |
116
|
|
|
throw new RelationException("HasMany relation can only be inversed into BelongsTo or RefersTo"); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
if (!empty($this->options->get(Relation::WHERE))) { |
120
|
|
|
throw new RelationException("Unable to inverse HasMany relation with where constrain"); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return $relation->withContext( |
124
|
|
|
$into, |
125
|
|
|
$this->target, |
126
|
|
|
$this->source, |
127
|
|
|
$this->options->withOptions([ |
128
|
|
|
Relation::LOAD => $load, |
129
|
|
|
Relation::INNER_KEY => $this->options->get(Relation::OUTER_KEY), |
130
|
|
|
Relation::OUTER_KEY => $this->options->get(Relation::INNER_KEY), |
131
|
|
|
]) |
132
|
|
|
); |
133
|
|
|
} |
134
|
|
|
} |