|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Spiral Framework. |
|
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\Registry; |
|
14
|
|
|
use Cycle\Schema\Relation\Traits\ForeignKeyTrait; |
|
15
|
|
|
|
|
16
|
|
|
final class Embedded extends RelationSchema |
|
17
|
|
|
{ |
|
18
|
|
|
use ForeignKeyTrait; |
|
19
|
|
|
|
|
20
|
|
|
// internal relation type |
|
21
|
|
|
protected const RELATION_TYPE = Relation::EMBEDDED; |
|
22
|
|
|
|
|
23
|
|
|
// relation schema options |
|
24
|
|
|
protected const RELATION_SCHEMA = [ |
|
25
|
|
|
Relation::LOAD => Relation::LOAD_EAGER, |
|
26
|
|
|
]; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param Registry $registry |
|
30
|
|
|
*/ |
|
31
|
|
|
public function compute(Registry $registry) |
|
32
|
|
|
{ |
|
33
|
|
|
$source = $registry->getEntity($this->source); |
|
34
|
|
|
$target = $registry->getEntity($this->target); |
|
35
|
|
|
|
|
36
|
|
|
// each embedded entity must isolated |
|
37
|
|
|
$target = clone $target; |
|
38
|
|
|
$target->setRole($source->getRole() . '.' . $target->getRole()); |
|
39
|
|
|
|
|
40
|
|
|
// embedded entity must point to the same table as parent entity |
|
41
|
|
|
$registry->register($target); |
|
42
|
|
|
$registry->linkTable($target, $registry->getDatabase($source), $registry->getTable($source)); |
|
43
|
|
|
|
|
44
|
|
|
// isolated |
|
45
|
|
|
$this->target = $target->getRole(); |
|
46
|
|
|
|
|
47
|
|
|
foreach ($source->getFields() as $name => $field) { |
|
48
|
|
|
if ($field->isPrimary()) { |
|
49
|
|
|
// sync primary keys |
|
50
|
|
|
$target->getFields()->set($name, $field); |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
parent::compute($registry); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @param Registry $registry |
|
59
|
|
|
*/ |
|
60
|
|
|
public function render(Registry $registry) |
|
61
|
|
|
{ |
|
62
|
|
|
// relation does not require any column rendering besides actual tables |
|
63
|
|
|
} |
|
64
|
|
|
} |