Skip to content

Commit 2cf8641

Browse files
committed
Refactor duplicate code into protected methods.
Move duplicated code into protected methods. Fewer LOC is generally easier to maintain.
1 parent 67224bc commit 2cf8641

File tree

1 file changed

+27
-35
lines changed

1 file changed

+27
-35
lines changed

Cake/ORM/Associations.php

Lines changed: 27 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -98,30 +98,7 @@ public function saveParents(Table $table, Entity $entity, $associations, $option
9898
if (empty($associations)) {
9999
return $entity;
100100
}
101-
foreach ($associations as $alias => $nested) {
102-
if (is_int($alias)) {
103-
$alias = $nested;
104-
$nested = [];
105-
}
106-
$relation = $this->get($alias);
107-
if (!$relation) {
108-
$msg = __d(
109-
'cake_dev',
110-
'Cannot save %s, it is not associated to %s',
111-
$alias,
112-
$table->alias()
113-
);
114-
throw new \InvalidArgumentException($msg);
115-
}
116-
if ($relation->isOwningSide($table)) {
117-
continue;
118-
}
119-
if (!empty($nested)) {
120-
$options = (array)$nested + $options;
121-
}
122-
$this->_save($relation, $entity, $options);
123-
}
124-
return $entity;
101+
return $this->_saveAssociations($table, $entity, $associations, $options, false);
125102
}
126103

127104
/**
@@ -142,6 +119,21 @@ public function saveChildren(Table $table, Entity $entity, $associations, $optio
142119
if (empty($associations)) {
143120
return $entity;
144121
}
122+
return $this->_saveAssociations($table, $entity, $associations, $options, true);
123+
}
124+
125+
/**
126+
* Helper method for saving an association's data.
127+
*
128+
* @param Table $table The table the save is currently operating on
129+
* @param Entity $entity The entity to save
130+
* @param array $associations Array of associations to save.
131+
* @param array $options Original options
132+
* @param boolean $owningSide Compared with association classes'
133+
* isOwningSide method.
134+
* @return void
135+
*/
136+
protected function _saveAssociations($table, $entity, $associations, $options, $owningSide) {
145137
foreach ($associations as $alias => $nested) {
146138
if (is_int($alias)) {
147139
$alias = $nested;
@@ -157,30 +149,30 @@ public function saveChildren(Table $table, Entity $entity, $associations, $optio
157149
);
158150
throw new \InvalidArgumentException($msg);
159151
}
160-
if (!$relation->isOwningSide($table)) {
152+
if ($relation->isOwningSide($table) !== $owningSide) {
161153
continue;
162154
}
163-
if (!empty($nested)) {
164-
$options = (array)$nested + $options;
165-
}
166-
$this->_save($relation, $entity, $options);
155+
$this->_save($relation, $entity, $nested, $options);
167156
}
168157
return $entity;
169158
}
170159

171160
/**
172161
* Helper method for saving an association's data.
173162
*
174-
* @param Association $association
175-
* @param Entity $entity
176-
* @param array $options
163+
* @param Association $association The association object to save with.
164+
* @param Entity $entity The entity to save
165+
* @param array $nested Options for deeper associations
166+
* @param array $options Original options
177167
* @return void
178168
*/
179-
protected function _save($association, $entity, $options) {
180-
$property = $association->property();
181-
if (!$entity->dirty($property)) {
169+
protected function _save($association, $entity, $nested, $options) {
170+
if (!$entity->dirty($association->property())) {
182171
return;
183172
}
173+
if (!empty($nested)) {
174+
$options = (array)$nested + $options;
175+
}
184176
$association->save($entity, $options);
185177
}
186178

0 commit comments

Comments
 (0)