@@ -98,6 +98,18 @@ public function initRelation(array $models, $relation)
9898 */
9999 public function match (array $ models , Collection $ results , $ relation )
100100 {
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+
101113 return $ models ;
102114 }
103115
@@ -109,22 +121,9 @@ public function match(array $models, Collection $results, $relation)
109121 public function getResults ()
110122 {
111123 // Get embedded documents.
112- $ results = $ this ->getEmbedded ();
113-
114- $ models = array ();
124+ $ results = $ this ->getEmbeddedRecords ();
115125
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 );
128127 }
129128
130129 /**
@@ -187,12 +186,12 @@ protected function performInsert(Model $model)
187186 $ result = $ this ->query ->push ($ this ->localKey , $ model ->getAttributes (), true );
188187
189188 // Get existing embedded documents.
190- $ documents = $ this ->getEmbedded ();
189+ $ documents = $ this ->getEmbeddedRecords ();
191190
192191 // Add the document to the parent model.
193192 $ documents [] = $ model ->getAttributes ();
194193
195- $ this ->setEmbedded ($ documents );
194+ $ this ->setEmbeddedRecords ($ documents );
196195
197196 return $ result ? $ model : false ;
198197 }
@@ -221,7 +220,7 @@ protected function performUpdate(Model $model)
221220 ->update (array ($ this ->localKey . '.$ ' => $ model ->getAttributes ()));
222221
223222 // Get existing embedded documents.
224- $ documents = $ this ->getEmbedded ();
223+ $ documents = $ this ->getEmbeddedRecords ();
225224
226225 $ primaryKey = $ this ->related ->getKeyName ();
227226
@@ -237,7 +236,7 @@ protected function performUpdate(Model $model)
237236 }
238237 }
239238
240- $ this ->setEmbedded ($ documents );
239+ $ this ->setEmbeddedRecords ($ documents );
241240
242241 return $ result ? $ model : false ;
243242 }
@@ -325,7 +324,7 @@ public function destroy($ids = array())
325324 }
326325
327326 // Get existing embedded documents.
328- $ documents = $ this ->getEmbedded ();
327+ $ documents = $ this ->getEmbeddedRecords ();
329328
330329 // Remove the document from the parent model.
331330 foreach ($ documents as $ i => $ document )
@@ -336,7 +335,7 @@ public function destroy($ids = array())
336335 }
337336 }
338337
339- $ this ->setEmbedded ($ documents );
338+ $ this ->setEmbeddedRecords ($ documents );
340339
341340 return $ count ;
342341 }
@@ -364,11 +363,35 @@ public function attach(Model $model)
364363 }
365364
366365 /**
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.
368391 *
369392 * @return array
370393 */
371- public function getEmbedded ()
394+ protected function getEmbeddedRecords ()
372395 {
373396 // Get raw attributes to skip relations and accessors.
374397 $ attributes = $ this ->parent ->getAttributes ();
@@ -377,11 +400,11 @@ public function getEmbedded()
377400 }
378401
379402 /**
380- * Set the embedded documents array
403+ * Set the embedded documents array.
381404 *
382405 * @param array $models
383406 */
384- public function setEmbedded (array $ models )
407+ protected function setEmbeddedRecords (array $ models )
385408 {
386409 $ attributes = $ this ->parent ->getAttributes ();
387410
0 commit comments