@@ -98,30 +98,7 @@ public function saveParents(Table $table, Entity $entity, $associations, $option
98
98
if (empty ($ associations )) {
99
99
return $ entity ;
100
100
}
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 );
125
102
}
126
103
127
104
/**
@@ -142,6 +119,21 @@ public function saveChildren(Table $table, Entity $entity, $associations, $optio
142
119
if (empty ($ associations )) {
143
120
return $ entity ;
144
121
}
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 ) {
145
137
foreach ($ associations as $ alias => $ nested ) {
146
138
if (is_int ($ alias )) {
147
139
$ alias = $ nested ;
@@ -157,30 +149,30 @@ public function saveChildren(Table $table, Entity $entity, $associations, $optio
157
149
);
158
150
throw new \InvalidArgumentException ($ msg );
159
151
}
160
- if (! $ relation ->isOwningSide ($ table )) {
152
+ if ($ relation ->isOwningSide ($ table ) !== $ owningSide ) {
161
153
continue ;
162
154
}
163
- if (!empty ($ nested )) {
164
- $ options = (array )$ nested + $ options ;
165
- }
166
- $ this ->_save ($ relation , $ entity , $ options );
155
+ $ this ->_save ($ relation , $ entity , $ nested , $ options );
167
156
}
168
157
return $ entity ;
169
158
}
170
159
171
160
/**
172
161
* Helper method for saving an association's data.
173
162
*
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
177
167
* @return void
178
168
*/
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 ())) {
182
171
return ;
183
172
}
173
+ if (!empty ($ nested )) {
174
+ $ options = (array )$ nested + $ options ;
175
+ }
184
176
$ association ->save ($ entity , $ options );
185
177
}
186
178
0 commit comments