1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Storage package |
5
|
|
|
* |
6
|
|
|
* (c) Michal Wachowski <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Moss\Storage\Query\Relation; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* One to one relation handler with mediator (pivot) table |
16
|
|
|
* |
17
|
|
|
* @author Michal Wachowski <[email protected]> |
18
|
|
|
* @package Moss\Storage |
19
|
|
|
*/ |
20
|
|
|
class OneTroughRelation extends AbstractRelation implements RelationInterface |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Executes read for one-to-one relation |
24
|
|
|
* |
25
|
|
|
* @param array $result |
26
|
|
|
* |
27
|
|
|
* @return array |
28
|
|
|
*/ |
29
|
|
View Code Duplication |
public function read(&$result) |
|
|
|
|
30
|
|
|
{ |
31
|
|
|
$relations = []; |
32
|
|
|
$conditions = []; |
33
|
|
|
|
34
|
|
|
foreach ($result as $i => $entity) { |
35
|
|
|
foreach ($this->definition->localKeys() as $local => $refer) { |
36
|
|
|
$conditions[$refer][] = $this->accessor->getPropertyValue($entity, $local); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
$relations[$this->buildLocalKey($entity, $this->definition->localKeys())][] = & $result[$i]; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
$collection = $this->fetch($this->definition->mediator(), $conditions, false); |
43
|
|
|
|
44
|
|
|
$mediator = []; |
45
|
|
|
$conditions = []; |
46
|
|
|
foreach ($collection as $entity) { |
47
|
|
|
foreach ($this->definition->foreignKeys() as $local => $refer) { |
48
|
|
|
$conditions[$refer][] = $this->accessor->getPropertyValue($entity, $local); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$in = $this->buildForeignKey($entity, $this->definition->localKeys()); |
52
|
|
|
$out = $this->buildLocalKey($entity, $this->definition->foreignKeys()); |
53
|
|
|
$mediator[$out] = $in; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$collection = $this->fetch($this->definition->entity(), $conditions, true); |
57
|
|
|
|
58
|
|
|
foreach ($collection as $relEntity) { |
59
|
|
|
$key = $this->buildForeignKey($relEntity, $this->definition->foreignKeys()); |
60
|
|
|
|
61
|
|
|
if (!isset($mediator[$key]) || !isset($relations[$mediator[$key]])) { |
62
|
|
|
continue; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
foreach ($relations[$mediator[$key]] as &$entity) { |
66
|
|
|
$this->accessor->setPropertyValue($entity, $this->definition->container(), $relEntity); |
67
|
|
|
unset($entity); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return $result; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Executes write fro one-to-one relation |
76
|
|
|
* |
77
|
|
|
* @param array|\ArrayAccess $result |
78
|
|
|
* |
79
|
|
|
* @return array|\ArrayAccess |
80
|
|
|
*/ |
81
|
|
|
public function write(&$result) |
82
|
|
|
{ |
83
|
|
|
$entity = $this->accessor->getPropertyValue($result, $this->definition->container()); |
84
|
|
View Code Duplication |
if (empty($entity)) { |
|
|
|
|
85
|
|
|
$conditions = []; |
86
|
|
|
foreach ($this->definition->localKeys() as $local => $foreign) { |
87
|
|
|
$conditions[$foreign][] = $this->accessor->getPropertyValue($result, $local); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$this->cleanup($this->definition->mediator(), [], $conditions); |
91
|
|
|
return $result; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$this->storage->write($entity, $this->definition->entity())->execute(); |
95
|
|
|
|
96
|
|
|
$mediator = []; |
97
|
|
|
|
98
|
|
|
foreach ($this->definition->localKeys() as $local => $foreign) { |
99
|
|
|
$mediator[$foreign] = $this->accessor->getPropertyValue($result, $local); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
foreach ($this->definition->foreignKeys() as $foreign => $local) { |
103
|
|
|
$mediator[$foreign] = $this->accessor->getPropertyValue($entity, $local); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
$this->storage->write($mediator, $this->definition->mediator())->execute(); |
107
|
|
|
$this->accessor->setPropertyValue($result, $this->definition->container(), $entity); |
108
|
|
|
|
109
|
|
|
$conditions = []; |
110
|
|
|
foreach ($this->definition->localKeys() as $foreign) { |
111
|
|
|
$conditions[$foreign][] = $this->accessor->getPropertyValue($mediator, $foreign); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return $result; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Executes delete for one-to-one relation |
119
|
|
|
* |
120
|
|
|
* @param array|\ArrayAccess $result |
121
|
|
|
* |
122
|
|
|
* @return array|\ArrayAccess |
123
|
|
|
*/ |
124
|
|
View Code Duplication |
public function delete(&$result) |
|
|
|
|
125
|
|
|
{ |
126
|
|
|
$entity = $this->accessor->getPropertyValue($result, $this->definition->container()); |
127
|
|
|
if (empty($entity)) { |
128
|
|
|
return $result; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
$mediator = []; |
132
|
|
|
|
133
|
|
|
foreach ($this->definition->localKeys() as $entityField => $mediatorField) { |
134
|
|
|
$mediator[$mediatorField] = $this->accessor->getPropertyValue($result, $entityField); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
foreach ($this->definition->foreignKeys() as $mediatorField => $entityField) { |
138
|
|
|
$mediator[$mediatorField] = $this->accessor->getPropertyValue($entity, $entityField); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
$this->storage->delete($mediator, $this->definition->mediator())->execute(); |
142
|
|
|
$this->accessor->setPropertyValue($result, $this->definition->container(), $entity); |
143
|
|
|
|
144
|
|
|
return $result; |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.