11<?php namespace Jenssegers \Mongodb \Relations ;
22
33use Illuminate \Database \Eloquent \Builder ;
4- use Illuminate \Database \Eloquent \Collection as BaseCollection ;
4+ use Illuminate \Database \Eloquent \Collection ;
55use Illuminate \Database \Eloquent \Model ;
66use Illuminate \Database \Eloquent \Relations \Relation ;
7- use Jenssegers \Mongodb \Eloquent \Collection ;
87
98abstract class EmbedsOneOrMany extends Relation {
109
@@ -104,7 +103,7 @@ public function initRelation(array $models, $relation)
104103 * @param string $relation
105104 * @return array
106105 */
107- public function match (array $ models , BaseCollection $ results , $ relation )
106+ public function match (array $ models , Collection $ results , $ relation )
108107 {
109108 foreach ($ models as $ model )
110109 {
@@ -121,7 +120,7 @@ public function match(array $models, BaseCollection $results, $relation)
121120 /**
122121 * Shorthand to get the results of the relationship.
123122 *
124- * @return \Jenssegers\Mongodb \Eloquent\Collection
123+ * @return \Illuminate\Database \Eloquent\Collection
125124 */
126125 public function get ()
127126 {
@@ -212,7 +211,7 @@ public function createMany(array $records)
212211 */
213212 protected function getIdsArrayFrom ($ ids )
214213 {
215- if ($ ids instanceof Collection)
214+ if ($ ids instanceof \ Illuminate \ Support \ Collection)
216215 {
217216 $ ids = $ ids ->all ();
218217 }
@@ -237,7 +236,9 @@ protected function getEmbedded()
237236 // Get raw attributes to skip relations and accessors.
238237 $ attributes = $ this ->parent ->getAttributes ();
239238
240- return isset ($ attributes [$ this ->localKey ]) ? $ attributes [$ this ->localKey ] : null ;
239+ $ embedded = isset ($ attributes [$ this ->localKey ]) ? (array ) $ attributes [$ this ->localKey ] : [];
240+
241+ return $ embedded ;
241242 }
242243
243244 /**
@@ -248,6 +249,7 @@ protected function getEmbedded()
248249 */
249250 protected function setEmbedded ($ records )
250251 {
252+ // Assign models to parent attributes array.
251253 $ attributes = $ this ->parent ->getAttributes ();
252254
253255 $ attributes [$ this ->localKey ] = $ records ;
@@ -256,7 +258,7 @@ protected function setEmbedded($records)
256258 $ this ->parent ->setRawAttributes ($ attributes );
257259
258260 // Set the relation on the parent.
259- return $ this ->parent ->setRelation ($ this ->relation , $ this ->getResults ());
261+ return $ this ->parent ->setRelation ($ this ->relation , $ records === null ? null : $ this ->getResults ());
260262 }
261263
262264 /**
@@ -280,7 +282,7 @@ protected function getForeignKeyValue($id)
280282 * Convert an array of records to a Collection.
281283 *
282284 * @param array $records
283- * @return \Jenssegers\Mongodb \Eloquent\Collection
285+ * @return \Illuminate\Database \Eloquent\Collection
284286 */
285287 protected function toCollection (array $ records = [])
286288 {
0 commit comments