@@ -98,6 +98,18 @@ public function initRelation(array $models, $relation)
98
98
*/
99
99
public function match (array $ models , Collection $ results , $ relation )
100
100
{
101
+ foreach ($ models as $ model )
102
+ {
103
+ // Get raw attributes to skip relations and accessors.
104
+ $ attributes = $ model ->getAttributes ();
105
+
106
+ $ results = isset ($ attributes [$ this ->localKey ]) ? $ attributes [$ this ->localKey ] : array ();
107
+
108
+ $ collection = $ this ->toCollection ($ results );
109
+
110
+ $ model ->setRelation ($ relation , $ collection );
111
+ }
112
+
101
113
return $ models ;
102
114
}
103
115
@@ -109,22 +121,9 @@ public function match(array $models, Collection $results, $relation)
109
121
public function getResults ()
110
122
{
111
123
// Get embedded documents.
112
- $ results = $ this ->getEmbedded ();
113
-
114
- $ models = array ();
124
+ $ results = $ this ->getEmbeddedRecords ();
115
125
116
- // Wrap documents in model objects.
117
- foreach ($ results as $ result )
118
- {
119
- $ model = $ this ->related ->newFromBuilder ($ result );
120
-
121
- // Attatch the parent relation to the embedded model.
122
- $ model ->setRelation ($ this ->foreignKey , $ this ->parent );
123
-
124
- $ models [] = $ model ;
125
- }
126
-
127
- return $ this ->related ->newCollection ($ models );
126
+ return $ this ->toCollection ($ results );
128
127
}
129
128
130
129
/**
@@ -187,12 +186,12 @@ protected function performInsert(Model $model)
187
186
$ result = $ this ->query ->push ($ this ->localKey , $ model ->getAttributes (), true );
188
187
189
188
// Get existing embedded documents.
190
- $ documents = $ this ->getEmbedded ();
189
+ $ documents = $ this ->getEmbeddedRecords ();
191
190
192
191
// Add the document to the parent model.
193
192
$ documents [] = $ model ->getAttributes ();
194
193
195
- $ this ->setEmbedded ($ documents );
194
+ $ this ->setEmbeddedRecords ($ documents );
196
195
197
196
return $ result ? $ model : false ;
198
197
}
@@ -221,7 +220,7 @@ protected function performUpdate(Model $model)
221
220
->update (array ($ this ->localKey . '.$ ' => $ model ->getAttributes ()));
222
221
223
222
// Get existing embedded documents.
224
- $ documents = $ this ->getEmbedded ();
223
+ $ documents = $ this ->getEmbeddedRecords ();
225
224
226
225
$ primaryKey = $ this ->related ->getKeyName ();
227
226
@@ -237,7 +236,7 @@ protected function performUpdate(Model $model)
237
236
}
238
237
}
239
238
240
- $ this ->setEmbedded ($ documents );
239
+ $ this ->setEmbeddedRecords ($ documents );
241
240
242
241
return $ result ? $ model : false ;
243
242
}
@@ -325,7 +324,7 @@ public function destroy($ids = array())
325
324
}
326
325
327
326
// Get existing embedded documents.
328
- $ documents = $ this ->getEmbedded ();
327
+ $ documents = $ this ->getEmbeddedRecords ();
329
328
330
329
// Remove the document from the parent model.
331
330
foreach ($ documents as $ i => $ document )
@@ -336,7 +335,7 @@ public function destroy($ids = array())
336
335
}
337
336
}
338
337
339
- $ this ->setEmbedded ($ documents );
338
+ $ this ->setEmbeddedRecords ($ documents );
340
339
341
340
return $ count ;
342
341
}
@@ -364,11 +363,35 @@ public function attach(Model $model)
364
363
}
365
364
366
365
/**
367
- * Get the embedded documents array
366
+ * Convert an array of embedded documents to a Collection.
367
+ *
368
+ * @param array $results
369
+ * @return Illuminate\Database\Eloquent\Collection
370
+ */
371
+ protected function toCollection (array $ results = array ())
372
+ {
373
+ $ models = array ();
374
+
375
+ // Wrap documents in model objects.
376
+ foreach ($ results as $ result )
377
+ {
378
+ $ model = $ this ->related ->newFromBuilder ($ result );
379
+
380
+ // Attatch the parent relation to the embedded model.
381
+ $ model ->setRelation ($ this ->foreignKey , $ this ->parent );
382
+
383
+ $ models [] = $ model ;
384
+ }
385
+
386
+ return $ this ->related ->newCollection ($ models );
387
+ }
388
+
389
+ /**
390
+ * Get the embedded documents array.
368
391
*
369
392
* @return array
370
393
*/
371
- public function getEmbedded ()
394
+ protected function getEmbeddedRecords ()
372
395
{
373
396
// Get raw attributes to skip relations and accessors.
374
397
$ attributes = $ this ->parent ->getAttributes ();
@@ -377,11 +400,11 @@ public function getEmbedded()
377
400
}
378
401
379
402
/**
380
- * Set the embedded documents array
403
+ * Set the embedded documents array.
381
404
*
382
405
* @param array $models
383
406
*/
384
- public function setEmbedded (array $ models )
407
+ protected function setEmbeddedRecords (array $ models )
385
408
{
386
409
$ attributes = $ this ->parent ->getAttributes ();
387
410
0 commit comments