1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Cycle\Schema\Relation\Morphed; |
6
|
|
|
|
7
|
|
|
use Cycle\ORM\Relation; |
8
|
|
|
use Cycle\Schema\Registry; |
9
|
|
|
use Cycle\Schema\Relation\RelationSchema; |
10
|
|
|
use Cycle\Schema\Relation\Traits\FieldTrait; |
11
|
|
|
use Cycle\Schema\Relation\Traits\MorphTrait; |
12
|
|
|
|
13
|
|
|
final class MorphedHasMany extends RelationSchema |
14
|
|
|
{ |
15
|
|
|
use FieldTrait; |
16
|
|
|
use MorphTrait; |
17
|
|
|
|
18
|
|
|
// internal relation type |
19
|
|
|
protected const RELATION_TYPE = Relation::MORPHED_HAS_MANY; |
20
|
|
|
|
21
|
|
|
// relation schema options |
22
|
|
|
protected const RELATION_SCHEMA = [ |
23
|
|
|
// save with parent |
24
|
|
|
Relation::CASCADE => true, |
25
|
|
|
|
26
|
|
|
// do not pre-load relation by default |
27
|
|
|
Relation::LOAD => Relation::LOAD_PROMISE, |
28
|
|
|
|
29
|
|
|
// nullable by default |
30
|
|
|
Relation::NULLABLE => true, |
31
|
|
|
|
32
|
|
|
// default field name for inner key |
33
|
|
|
Relation::OUTER_KEY => '{relation}_{source:primaryKey}', |
34
|
|
|
|
35
|
|
|
// link to parent entity primary key by default |
36
|
|
|
Relation::INNER_KEY => '{source:primaryKey}', |
37
|
|
|
|
38
|
|
|
// link to parent entity primary key by default |
39
|
|
|
Relation::MORPH_KEY => '{relation}_role', |
40
|
|
|
|
41
|
|
|
// default collection. |
42
|
|
|
Relation::COLLECTION_TYPE => null, |
43
|
|
|
|
44
|
|
|
// rendering options |
45
|
|
|
RelationSchema::INDEX_CREATE => true, |
46
|
|
|
RelationSchema::MORPH_KEY_LENGTH => 32, |
47
|
|
|
]; |
48
|
|
|
|
49
|
|
|
public function compute(Registry $registry): void |
50
|
|
|
{ |
51
|
|
|
parent::compute($registry); |
52
|
48 |
|
|
53
|
|
|
$source = $registry->getEntity($this->source); |
54
|
48 |
|
$target = $registry->getEntity($this->target); |
55
|
|
|
|
56
|
48 |
|
// create target outer field |
57
|
48 |
|
$this->createRelatedFields( |
58
|
|
|
$source, |
59
|
|
|
Relation::INNER_KEY, |
60
|
48 |
|
$target, |
61
|
|
|
Relation::OUTER_KEY, |
62
|
48 |
|
); |
63
|
|
|
|
64
|
48 |
|
// create target outer field |
65
|
|
|
$this->ensureMorphField( |
66
|
|
|
$target, |
67
|
|
|
$this->options->get(Relation::MORPH_KEY), |
68
|
48 |
|
$this->options->get(RelationSchema::MORPH_KEY_LENGTH), |
69
|
|
|
$this->options->get(Relation::NULLABLE), |
70
|
48 |
|
); |
71
|
48 |
|
} |
72
|
48 |
|
|
73
|
|
|
public function render(Registry $registry): void |
74
|
48 |
|
{ |
75
|
|
|
$target = $registry->getEntity($this->target); |
76
|
16 |
|
$outerFields = $this->getFields($target, Relation::OUTER_KEY); |
77
|
|
|
$morphFields = $this->getFields($target, Relation::MORPH_KEY); |
78
|
16 |
|
|
79
|
16 |
|
$this->mergeIndex($registry, $target, $outerFields, $morphFields); |
80
|
16 |
|
} |
81
|
|
|
} |
82
|
|
|
|