|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Sirius\Orm\Relation; |
|
4
|
|
|
|
|
5
|
|
|
use Sirius\Orm\Action\BaseAction; |
|
6
|
|
|
use Sirius\Orm\Contract\EntityInterface; |
|
7
|
|
|
use Sirius\Orm\Contract\Relation\ToOneInterface; |
|
8
|
|
|
use Sirius\Orm\Entity\StateEnum; |
|
9
|
|
|
use Sirius\Orm\Entity\Tracker; |
|
10
|
|
|
|
|
11
|
|
|
class OneToOne extends OneToMany implements ToOneInterface |
|
12
|
|
|
{ |
|
13
|
|
|
public function attachMatchesToEntity(EntityInterface $nativeEntity, array $result) |
|
14
|
|
|
{ |
|
15
|
|
|
// no point in linking entities if the native one is deleted |
|
16
|
|
|
if ($nativeEntity->getState() == StateEnum::DELETED) { |
|
17
|
|
|
return; |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
$nativeId = $this->getEntityId($this->nativeMapper, $nativeEntity, array_keys($this->keyPairs)); |
|
21
|
|
|
|
|
22
|
|
|
$found = $result[$nativeId] ?? []; |
|
23
|
|
|
|
|
24
|
|
|
$this->nativeEntityHydrator->set($nativeEntity, $this->name, $found[0] ?? null); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
protected function addActionOnDelete(BaseAction $action) |
|
28
|
|
|
{ |
|
29
|
|
|
$relations = $action->getOption('relations'); |
|
30
|
|
|
|
|
31
|
|
|
// no cascade delete? treat it as a save |
|
32
|
|
|
if (! $this->isCascade()) { |
|
33
|
|
|
$this->addActionOnSave($action); |
|
34
|
|
|
} elseif ($relations === true || in_array($this->name, (array)$relations)) { |
|
35
|
|
|
$nativeEntity = $action->getEntity(); |
|
36
|
|
|
$remainingRelations = $this->getRemainingRelations($relations); |
|
37
|
|
|
|
|
38
|
|
|
// retrieve them again from the DB since the related collection might not have everything |
|
39
|
|
|
// for example due to a relation query callback |
|
40
|
|
|
$foreignEntity = $this->getQuery(new Tracker([$nativeEntity->toArray()])) |
|
41
|
|
|
->first(); |
|
42
|
|
|
|
|
43
|
|
|
if ($foreignEntity) { |
|
44
|
|
|
$deleteAction = $this->foreignMapper |
|
45
|
|
|
->newDeleteAction($foreignEntity, ['relations' => $remainingRelations]); |
|
|
|
|
|
|
46
|
|
|
$action->prepend($deleteAction); |
|
47
|
|
|
$action->append($this->newSyncAction($action->getEntity(), $foreignEntity, 'delete')); |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function detachEntities(EntityInterface $nativeEntity, EntityInterface $foreignEntity = null) |
|
53
|
|
|
{ |
|
54
|
|
|
if ($foreignEntity) { |
|
55
|
|
|
foreach ($this->keyPairs as $nativeCol => $foreignCol) { |
|
56
|
|
|
$this->foreignEntityHydrator->set($foreignEntity, $foreignCol, null); |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
$this->nativeEntityHydrator->set($nativeEntity, $this->name, null); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
protected function addActionOnSave(BaseAction $action) |
|
63
|
|
|
{ |
|
64
|
|
|
if (! $this->relationWasChanged($action->getEntity())) { |
|
65
|
|
|
return; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
if (! $action->includesRelation($this->name)) { |
|
69
|
|
|
return; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
$foreignEntity = $this->nativeEntityHydrator->get($action->getEntity(), $this->name); |
|
73
|
|
|
if ($foreignEntity) { |
|
74
|
|
|
$remainingRelations = $this->getRemainingRelations($action->getOption('relations')); |
|
75
|
|
|
|
|
76
|
|
|
$saveAction = $this->foreignMapper->newSaveAction($foreignEntity, ['relations' => $remainingRelations]); |
|
|
|
|
|
|
77
|
|
|
$saveAction->addColumns($this->getExtraColumnsForAction()); |
|
78
|
|
|
$action->prepend($saveAction); |
|
79
|
|
|
$action->append($this->newSyncAction($action->getEntity(), $foreignEntity, 'save')); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|