1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
/** |
3
|
|
|
* Spiral Framework. |
4
|
|
|
* |
5
|
|
|
* @license MIT |
6
|
|
|
* @author Anton Titov (Wolfy-J) |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Cycle\Schema\Relation\Morphed; |
10
|
|
|
|
11
|
|
|
use Cycle\ORM\Relation; |
12
|
|
|
use Cycle\Schema\Registry; |
13
|
|
|
use Cycle\Schema\Relation\RelationSchema; |
14
|
|
|
use Cycle\Schema\Relation\Traits\FieldTrait; |
15
|
|
|
use Cycle\Schema\Relation\Traits\MorphTrait; |
16
|
|
|
|
17
|
|
|
class BelongsToMorphed extends RelationSchema |
18
|
|
|
{ |
19
|
|
|
use FieldTrait, MorphTrait; |
20
|
|
|
|
21
|
|
|
// internal relation type |
22
|
|
|
protected const RELATION_TYPE = Relation::BELONGS_TO_MORPHED; |
23
|
|
|
|
24
|
|
|
// relation schema options |
25
|
|
|
protected const RELATION_SCHEMA = [ |
26
|
|
|
// nullable by default |
27
|
|
|
Relation::NULLABLE => true, |
28
|
|
|
|
29
|
|
|
// default field name for inner key |
30
|
|
|
Relation::OUTER_KEY => '{target:primaryKey}', |
31
|
|
|
|
32
|
|
|
// link to parent entity primary key by default |
33
|
|
|
Relation::INNER_KEY => '{relation}_{outerKey}', |
34
|
|
|
|
35
|
|
|
// link to parent entity primary key by default |
36
|
|
|
Relation::MORPH_KEY => '{relation}_role', |
37
|
|
|
|
38
|
|
|
// rendering options |
39
|
|
|
RelationSchema::INDEX_CREATE => true, |
40
|
|
|
RelationSchema::MORPH_KEY_LENGTH => 32 |
41
|
|
|
]; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param Registry $registry |
45
|
|
|
*/ |
46
|
|
|
public function compute(Registry $registry) |
47
|
|
|
{ |
48
|
|
|
// compute local key |
49
|
|
|
$this->options = $this->options->withContext([ |
50
|
|
|
'source:primaryKey' => $this->getPrimary($registry->getEntity($this->source)) |
51
|
|
|
]); |
52
|
|
|
|
53
|
|
|
$source = $registry->getEntity($this->source); |
54
|
|
|
|
55
|
|
|
list($outerKey, $outerField) = $this->findOuterKey($registry, $this->target); |
56
|
|
|
|
57
|
|
|
// register primary key reference |
58
|
|
|
$this->options = $this->options->withContext(['target:primaryKey' => $outerKey]); |
59
|
|
|
|
60
|
|
|
// create target outer field |
61
|
|
|
$this->ensureField( |
62
|
|
|
$source, |
63
|
|
|
$this->options->get(Relation::INNER_KEY), |
64
|
|
|
$outerField, |
65
|
|
|
$this->options->get(Relation::NULLABLE) |
|
|
|
|
66
|
|
|
); |
67
|
|
|
|
68
|
|
|
$this->ensureMorphField( |
69
|
|
|
$source, |
70
|
|
|
$this->options->get(Relation::MORPH_KEY), |
71
|
|
|
$this->options->get(RelationSchema::MORPH_KEY_LENGTH), |
|
|
|
|
72
|
|
|
$this->options->get(Relation::NULLABLE) |
|
|
|
|
73
|
|
|
); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param Registry $registry |
78
|
|
|
*/ |
79
|
|
|
public function render(Registry $registry) |
80
|
|
|
{ |
81
|
|
|
$source = $registry->getEntity($this->source); |
82
|
|
|
|
83
|
|
|
$innerField = $this->getField($source, Relation::INNER_KEY); |
84
|
|
|
$morphField = $this->getField($source, Relation::MORPH_KEY); |
85
|
|
|
|
86
|
|
|
$table = $registry->getTableSchema($source); |
87
|
|
|
|
88
|
|
|
if ($this->options->get(self::INDEX_CREATE)) { |
89
|
|
|
$table->index([$innerField->getColumn(), $morphField->getColumn()]); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
} |