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\Morphed; |
11
|
|
|
|
12
|
|
|
use Cycle\ORM\Relation; |
13
|
|
|
use Cycle\Schema\Registry; |
14
|
|
|
use Cycle\Schema\Relation\RelationSchema; |
15
|
|
|
use Cycle\Schema\Relation\Traits\FieldTrait; |
16
|
|
|
use Cycle\Schema\Relation\Traits\MorphTrait; |
17
|
|
|
|
18
|
|
|
class MorphedHasMany extends RelationSchema |
19
|
|
|
{ |
20
|
|
|
use FieldTrait, MorphTrait; |
21
|
|
|
|
22
|
|
|
// internal relation type |
23
|
|
|
protected const RELATION_TYPE = Relation::MORPHED_HAS_MANY; |
24
|
|
|
|
25
|
|
|
// relation schema options |
26
|
|
|
protected const RELATION_SCHEMA = [ |
27
|
|
|
// save with parent |
28
|
|
|
Relation::CASCADE => true, |
29
|
|
|
|
30
|
|
|
// do not pre-load relation by default |
31
|
|
|
Relation::LOAD => null, |
32
|
|
|
|
33
|
|
|
// nullable by default |
34
|
|
|
Relation::NULLABLE => true, |
35
|
|
|
|
36
|
|
|
// default field name for inner key |
37
|
|
|
Relation::OUTER_KEY => '{relation}_{source:primaryKey}', |
38
|
|
|
|
39
|
|
|
// link to parent entity primary key by default |
40
|
|
|
Relation::INNER_KEY => '{source:primaryKey}', |
41
|
|
|
|
42
|
|
|
// link to parent entity primary key by default |
43
|
|
|
Relation::MORPH_KEY => '{relation}_role', |
44
|
|
|
|
45
|
|
|
// rendering options |
46
|
|
|
RelationSchema::INDEX_CREATE => true, |
47
|
|
|
RelationSchema::MORPH_KEY_LENGTH => 32 |
48
|
|
|
]; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param Registry $registry |
52
|
|
|
*/ |
53
|
|
|
public function compute(Registry $registry) |
54
|
|
|
{ |
55
|
|
|
parent::compute($registry); |
56
|
|
|
|
57
|
|
|
$source = $registry->getEntity($this->source); |
58
|
|
|
$target = $registry->getEntity($this->target); |
59
|
|
|
|
60
|
|
|
// create target outer field |
61
|
|
|
$this->ensureField( |
62
|
|
|
$target, |
63
|
|
|
$this->options->get(Relation::OUTER_KEY), |
64
|
|
|
$this->getField($source, Relation::INNER_KEY), |
65
|
|
|
$this->options->get(Relation::NULLABLE) |
|
|
|
|
66
|
|
|
); |
67
|
|
|
|
68
|
|
|
// create target outer field |
69
|
|
|
$this->ensureMorphField( |
70
|
|
|
$target, |
71
|
|
|
$this->options->get(Relation::MORPH_KEY), |
72
|
|
|
$this->options->get(RelationSchema::MORPH_KEY_LENGTH), |
|
|
|
|
73
|
|
|
$this->options->get(Relation::NULLABLE) |
|
|
|
|
74
|
|
|
); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param Registry $registry |
79
|
|
|
*/ |
80
|
|
|
public function render(Registry $registry) |
81
|
|
|
{ |
82
|
|
|
$target = $registry->getEntity($this->target); |
83
|
|
|
|
84
|
|
|
$outerField = $this->getField($target, Relation::OUTER_KEY); |
85
|
|
|
$morphField = $this->getField($target, Relation::MORPH_KEY); |
86
|
|
|
|
87
|
|
|
$table = $registry->getTableSchema($target); |
88
|
|
|
|
89
|
|
|
if ($this->options->get(self::INDEX_CREATE)) { |
90
|
|
|
$table->index([$outerField->getColumn(), $morphField->getColumn()]); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|