/
Associations.php
234 lines (218 loc) · 6.47 KB
/
Associations.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since CakePHP(tm) v 3.0.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace Cake\ORM;
use Cake\ORM\Association;
use Cake\ORM\Entity;
use Cake\ORM\Table;
/**
* A container/collection for association classes.
*
* Contains methods for managing associations, and
* ordering operations around saving and deleting.
*/
class Associations {
/**
* Stored associations
*
* @var array
*/
protected $_items = [];
/**
* Add an association to the collection
*
* If the alias added contains a `.` the part preceding the `.` will be dropped.
* This makes using plugins simpler as the Plugin.Class syntax is frequently used.
*
* @param string $alias The association alias
* @param Association The association to add.
* @return Association The association object being added.
*/
public function add($alias, Association $association) {
list($plugin, $alias) = pluginSplit($alias);
return $this->_items[strtolower($alias)] = $association;
}
/**
* Fetch an attached association by name.
*
* @param string $alias The association alias to get.
* @return Association|null Either the association or null.
*/
public function get($alias) {
$alias = strtolower($alias);
if (isset($this->_items[$alias])) {
return $this->_items[$alias];
}
return null;
}
/**
* Fetch an association by property name.
*
* @param string $prop The property to find an association by.
* @return Association|null Either the association or null.
*/
public function getByProperty($prop) {
foreach ($this->_items as $assoc) {
if ($assoc->property() === $prop) {
return $assoc;
}
}
return null;
}
/**
* Check for an attached association by name.
*
* @param string $alias The association alias to get.
* @return boolean Whether or not the association exists.
*/
public function has($alias) {
return isset($this->_items[strtolower($alias)]);
}
/**
* Get the names of all the associations in the collection.
*
* @return array
*/
public function keys() {
return array_keys($this->_items);
}
/**
* Get an array of associations matching a specific type.
*
* @param string $class
* @return array
*/
public function type($class) {
$out = array_filter($this->_items, function ($assoc) use ($class) {
return strpos(get_class($assoc), $class) !== false;
});
return array_values($out);
}
/**
* Drop/remove an association.
*
* Once removed the association will not longer be reachable
*
* @param string The alias name.
* @return void
*/
public function remove($alias) {
unset($this->_items[strtolower($alias)]);
}
/**
* Save all the associations that are parents of the given entity.
*
* Parent associations include any association where the given table
* is the owning side.
*
* @param Table $table The table entity is for.
* @param Entity $entity The entity to save associated data for.
* @param array $associations The list of associations to save parents from.
* associations not in this list will not be saved.
* @param array $options The options for the save operation.
* @return boolean Success
*/
public function saveParents(Table $table, Entity $entity, $associations, $options = []) {
if (empty($associations)) {
return true;
}
return $this->_saveAssociations($table, $entity, $associations, $options, false);
}
/**
* Save all the associations that are children of the given entity.
*
* Child associations include any association where the given table
* is not the owning side.
*
* @param Table $table The table entity is for.
* @param Entity $entity The entity to save associated data for.
* @param array $associations The list of associations to save children from.
* associations not in this list will not be saved.
* @param array $options The options for the save operation.
* @return boolean Success
*/
public function saveChildren(Table $table, Entity $entity, $associations, $options) {
if (empty($associations)) {
return true;
}
return $this->_saveAssociations($table, $entity, $associations, $options, true);
}
/**
* Helper method for saving an association's data.
*
* @param Table $table The table the save is currently operating on
* @param Entity $entity The entity to save
* @param array $associations Array of associations to save.
* @param array $options Original options
* @param boolean $owningSide Compared with association classes'
* isOwningSide method.
* @return boolean Success
* @throws InvalidArgumentException When an unknown alias is used.
*/
protected function _saveAssociations($table, $entity, $associations, $options, $owningSide) {
unset($options['associated']);
foreach ($associations as $alias => $nested) {
if (is_int($alias)) {
$alias = $nested;
$nested = [];
}
$relation = $this->get($alias);
if (!$relation) {
$msg = sprintf(
'Cannot save %s, it is not associated to %s',
$alias,
$table->alias()
);
throw new \InvalidArgumentException($msg);
}
if ($relation->isOwningSide($table) !== $owningSide) {
continue;
}
if (!$this->_save($relation, $entity, $nested, $options)) {
return false;
}
}
return true;
}
/**
* Helper method for saving an association's data.
*
* @param Association $association The association object to save with.
* @param Entity $entity The entity to save
* @param array $nested Options for deeper associations
* @param array $options Original options
* @return boolean Success
*/
protected function _save($association, $entity, $nested, $options) {
if (!$entity->dirty($association->property())) {
return true;
}
if (!empty($nested)) {
$options = (array)$nested + $options;
}
return (bool)$association->save($entity, $options);
}
/**
* Cascade a delete across the various associations.
*
* @param Entity $entity The entity to delete associations for.
* @param array $options The options used in the delete operation.
* @return void
*/
public function cascadeDelete(Entity $entity, $options) {
foreach ($this->_items as $assoc) {
$assoc->cascadeDelete($entity, $options);
}
}
}