|
1
|
|
|
<?php |
|
2
|
|
|
namespace Darya\ORM\Relation; |
|
3
|
|
|
|
|
4
|
|
|
use Darya\ORM\Record; |
|
5
|
|
|
use Darya\ORM\Relation; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Darya's has-many entity relation. |
|
9
|
|
|
* |
|
10
|
|
|
* @author Chris Andrew <[email protected]> |
|
11
|
|
|
*/ |
|
12
|
|
|
class HasMany extends Has |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* Eagerly load the related models of the given parent instances. |
|
16
|
|
|
* |
|
17
|
|
|
* Retrieves the related models without matching them to their parents. |
|
18
|
|
|
* |
|
19
|
|
|
* @param array $instances |
|
20
|
|
|
* @return array |
|
21
|
|
|
*/ |
|
22
|
|
|
public function eagerLoad(array $instances) |
|
23
|
|
|
{ |
|
24
|
|
|
$this->verifyParents($instances); |
|
25
|
|
|
$ids = static::attributeList($instances, 'id'); |
|
26
|
|
|
|
|
27
|
|
|
$filter = array_merge($this->filter(), array( |
|
28
|
|
|
$this->foreignKey => array_unique($ids) |
|
29
|
|
|
)); |
|
30
|
|
|
|
|
31
|
|
|
$data = $this->storage()->read($this->target->table(), $filter, $this->order()); |
|
32
|
|
|
|
|
33
|
|
|
return $this->target->generate($data); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Eagerly load and match the related models for the given parent instances. |
|
38
|
|
|
* |
|
39
|
|
|
* Returns the given instances with their related models loaded. |
|
40
|
|
|
* |
|
41
|
|
|
* @param array $instances |
|
42
|
|
|
* @return array |
|
43
|
|
|
*/ |
|
44
|
|
|
public function eager(array $instances) |
|
45
|
|
|
{ |
|
46
|
|
|
if ($this->parent instanceof $this->target) { |
|
47
|
|
|
return $this->eagerSelf($instances); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
$related = $this->eagerLoad($instances); |
|
51
|
|
|
|
|
52
|
|
|
$instances = $this->match($instances, $related); |
|
53
|
|
|
|
|
54
|
|
|
return $instances; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Eagerly load the related models from the same table. |
|
59
|
|
|
* |
|
60
|
|
|
* This continues to load the same relation recursively. |
|
61
|
|
|
* |
|
62
|
|
|
* @param array $instances |
|
63
|
|
|
* @return array |
|
64
|
|
|
*/ |
|
65
|
|
|
protected function eagerSelf(array $instances) |
|
66
|
|
|
{ |
|
67
|
|
|
$parents = $instances; |
|
68
|
|
|
|
|
69
|
|
|
while ($related = $this->eagerLoad($parents)) { |
|
70
|
|
|
$this->match($parents, $related); |
|
71
|
|
|
|
|
72
|
|
|
$parents = $related; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
$this->match($parents, array()); |
|
76
|
|
|
|
|
77
|
|
|
return $instances; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Match the given related models to their parent instances. |
|
82
|
|
|
* |
|
83
|
|
|
* @param Record[] $instances |
|
84
|
|
|
* @param Record[] $related |
|
85
|
|
|
* @return Record[] |
|
86
|
|
|
*/ |
|
87
|
|
|
protected function match(array $instances, array $related) |
|
88
|
|
|
{ |
|
89
|
|
|
$list = $this->adjacencyList($related); |
|
90
|
|
|
|
|
91
|
|
|
foreach ($instances as $instance) { |
|
92
|
|
|
$key = $instance->id(); |
|
93
|
|
|
$value = isset($list[$key]) ? $list[$key] : array(); |
|
94
|
|
|
$instance->relation($this->name)->set($value); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
return $instances; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Associate the given models. |
|
102
|
|
|
* |
|
103
|
|
|
* Loads any currently associated models before attaching and saving |
|
104
|
|
|
* the given models. |
|
105
|
|
|
* |
|
106
|
|
|
* Returns the number of successfully associated models. |
|
107
|
|
|
* |
|
108
|
|
|
* @param Record[]|Record $instances |
|
109
|
|
|
* @return int |
|
110
|
|
|
*/ |
|
111
|
|
View Code Duplication |
public function associate($instances) |
|
|
|
|
|
|
112
|
|
|
{ |
|
113
|
|
|
$this->verify($instances); |
|
114
|
|
|
|
|
115
|
|
|
$this->load(); |
|
116
|
|
|
|
|
117
|
|
|
$this->attach($instances); |
|
118
|
|
|
|
|
119
|
|
|
$ids = static::attributeList($instances, 'id'); |
|
120
|
|
|
|
|
121
|
|
|
$successful = 0; |
|
122
|
|
|
|
|
123
|
|
|
foreach ($this->related as $model) { |
|
124
|
|
|
$this->persist($model); |
|
125
|
|
|
|
|
126
|
|
|
if (!$ids || in_array($model->id(), $ids)) { |
|
127
|
|
|
$model->set($this->foreignKey, $this->parent->id()); |
|
128
|
|
|
$successful += $model->save(); |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
return (int) $successful; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Retrieve the related models. |
|
137
|
|
|
* |
|
138
|
|
|
* @return Record[] |
|
139
|
|
|
*/ |
|
140
|
|
|
public function retrieve() |
|
141
|
|
|
{ |
|
142
|
|
|
return $this->all(); |
|
|
|
|
|
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Dissociate all currently associated models. |
|
147
|
|
|
* |
|
148
|
|
|
* Returns the number of models successfully dissociated. |
|
149
|
|
|
* |
|
150
|
|
|
* TODO: Consider constraints |
|
151
|
|
|
* |
|
152
|
|
|
* @return int |
|
153
|
|
|
*/ |
|
154
|
|
|
public function purge() |
|
155
|
|
|
{ |
|
156
|
|
|
$this->related = array(); |
|
157
|
|
|
|
|
158
|
|
|
// return (int) $this->storage()->query($this->target->table()) |
|
|
|
|
|
|
159
|
|
|
// ->where($this->foreignKey, 0) |
|
|
|
|
|
|
160
|
|
|
// ->update(array( |
|
|
|
|
|
|
161
|
|
|
// $this->foreignKey => $this->parent->get($this->localKey) |
|
|
|
|
|
|
162
|
|
|
// )) |
|
163
|
|
|
// ->cheers()->affected; |
|
|
|
|
|
|
164
|
|
|
|
|
165
|
|
|
return (int) $this->storage()->update($this->target->table(), array( |
|
166
|
|
|
$this->foreignKey => 0 |
|
167
|
|
|
), array( |
|
168
|
|
|
$this->foreignKey => $this->parent->get($this->localKey) |
|
169
|
|
|
)); |
|
170
|
|
|
} |
|
171
|
|
|
} |
|
172
|
|
|
|
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.