@@ -140,6 +140,8 @@ public function get()
140
140
*/
141
141
public function save (Model $ model )
142
142
{
143
+ $ this ->updateTimestamps ($ model );
144
+
143
145
// Insert a new document.
144
146
if ( ! $ model ->exists )
145
147
{
@@ -154,34 +156,40 @@ public function save(Model $model)
154
156
}
155
157
156
158
/**
157
- * Perform a model insert operation .
159
+ * Attach a model instance to the parent model without persistence .
158
160
*
159
161
* @param \Illuminate\Database\Eloquent\Model $model
160
162
* @return \Illuminate\Database\Eloquent\Model
161
163
*/
162
- protected function performInsert (Model $ model )
164
+ public function associate (Model $ model )
163
165
{
164
- // Create a new key.
165
- if ( ! $ model ->getAttribute ( ' _id ' ) )
166
+ // Insert the related model in the parent instance
167
+ if ( ! $ model ->exists )
166
168
{
167
- $ model -> setAttribute ( ' _id ' , new MongoId );
169
+ return $ this -> associateNew ( $ model );
168
170
}
169
171
170
- // Update timestamps.
171
- $ this ->updateTimestamps ($ model );
172
-
173
- // Push the document to the database.
174
- $ result = $ this ->query ->push ($ this ->localKey , $ model ->getAttributes (), true );
175
-
176
- $ documents = $ this ->getEmbeddedRecords ();
172
+ // Update the related model in the parent instance
173
+ else
174
+ {
175
+ return $ this ->associateExisting ($ model );
176
+ }
177
177
178
- // Add the document to the parent model.
179
- $ documents [] = $ model ->getAttributes ();
178
+ }
180
179
181
- $ this ->setEmbeddedRecords ($ documents );
180
+ /**
181
+ * Perform a model insert operation.
182
+ *
183
+ * @param \Illuminate\Database\Eloquent\Model $model
184
+ * @return \Illuminate\Database\Eloquent\Model
185
+ */
186
+ protected function performInsert (Model $ model )
187
+ {
188
+ // Insert the related model in the parent instance
189
+ $ this ->associateNew ($ model );
182
190
183
- // Mark the model as existing .
184
- $ model ->exists = true ;
191
+ // Push the document to the database .
192
+ $ result = $ this -> query -> push ( $ this -> localKey , $ model ->getAttributes (), true ) ;
185
193
186
194
return $ result ? $ model : false ;
187
195
}
@@ -194,8 +202,8 @@ protected function performInsert(Model $model)
194
202
*/
195
203
protected function performUpdate (Model $ model )
196
204
{
197
- // Update timestamps.
198
- $ this ->updateTimestamps ($ model );
205
+ // Update the related model in the parent instance
206
+ $ this ->associateExisting ($ model );
199
207
200
208
// Get the correct foreign key value.
201
209
$ id = $ this ->getForeignKeyValue ($ model ->getKey ());
@@ -204,6 +212,44 @@ protected function performUpdate(Model $model)
204
212
$ result = $ this ->query ->where ($ this ->localKey . '. ' . $ model ->getKeyName (), $ id )
205
213
->update (array ($ this ->localKey . '.$ ' => $ model ->getAttributes ()));
206
214
215
+ return $ result ? $ model : false ;
216
+ }
217
+
218
+ /**
219
+ * Attach a new model without persistence
220
+ *
221
+ * @param \Illuminate\Database\Eloquent\Model $model
222
+ * @return \Illuminate\Database\Eloquent\Model
223
+ */
224
+ protected function associateNew ($ model )
225
+ {
226
+ // Create a new key.
227
+ if ( ! $ model ->getAttribute ('_id ' ))
228
+ {
229
+ $ model ->setAttribute ('_id ' , new MongoId );
230
+ }
231
+
232
+ $ documents = $ this ->getEmbeddedRecords ();
233
+
234
+ // Add the document to the parent model.
235
+ $ documents [] = $ model ->getAttributes ();
236
+
237
+ $ this ->setEmbeddedRecords ($ documents );
238
+
239
+ // Mark the model as existing.
240
+ $ model ->exists = true ;
241
+
242
+ return $ model ;
243
+ }
244
+
245
+ /**
246
+ * Update an existing model without persistence
247
+ *
248
+ * @param \Illuminate\Database\Eloquent\Model $model
249
+ * @return \Illuminate\Database\Eloquent\Model
250
+ */
251
+ protected function associateExisting ($ model )
252
+ {
207
253
// Get existing embedded documents.
208
254
$ documents = $ this ->getEmbeddedRecords ();
209
255
@@ -212,18 +258,18 @@ protected function performUpdate(Model $model)
212
258
$ key = $ model ->getKey ();
213
259
214
260
// Replace the document in the parent model.
215
- foreach ($ documents as $ i => $ document )
261
+ foreach ($ documents as & $ document )
216
262
{
217
263
if ($ document [$ primaryKey ] == $ key )
218
264
{
219
- $ documents [ $ i ] = $ model ->getAttributes ();
265
+ $ document = $ model ->getAttributes ();
220
266
break ;
221
267
}
222
268
}
223
269
224
270
$ this ->setEmbeddedRecords ($ documents );
225
271
226
- return $ result ? $ model : false ;
272
+ return $ model ;
227
273
}
228
274
229
275
/**
0 commit comments