@@ -69,6 +69,7 @@ public function addConstraints()
69
69
*/
70
70
public function addEagerConstraints (array $ models )
71
71
{
72
+ // There are no eager loading constraints.
72
73
}
73
74
74
75
/**
@@ -100,14 +101,9 @@ public function match(array $models, Collection $results, $relation)
100
101
{
101
102
foreach ($ models as $ model )
102
103
{
103
- // Get raw attributes to skip relations and accessors.
104
- $ attributes = $ model ->getAttributes ();
104
+ $ results = $ this ->getEmbeddedRecords ($ model );
105
105
106
- $ results = isset ($ attributes [$ this ->localKey ]) ? $ attributes [$ this ->localKey ] : array ();
107
-
108
- $ collection = $ this ->toCollection ($ results );
109
-
110
- $ model ->setRelation ($ relation , $ collection );
106
+ $ model ->setRelation ($ relation , $ this ->toCollection ($ results ));
111
107
}
112
108
113
109
return $ models ;
@@ -120,10 +116,7 @@ public function match(array $models, Collection $results, $relation)
120
116
*/
121
117
public function getResults ()
122
118
{
123
- // Get embedded documents.
124
- $ results = $ this ->getEmbeddedRecords ();
125
-
126
- return $ this ->toCollection ($ results );
119
+ return $ this ->toCollection ($ this ->getEmbeddedRecords ());
127
120
}
128
121
129
122
/**
@@ -171,28 +164,22 @@ protected function performInsert(Model $model)
171
164
$ model ->setAttribute ('_id ' , new MongoId );
172
165
}
173
166
174
- // Set timestamps.
175
- if ($ model ->usesTimestamps ())
176
- {
177
- $ time = $ model ->freshTimestamp ();
178
-
179
- $ model ->setUpdatedAt ($ time );
180
- $ model ->setCreatedAt ($ time );
181
- }
182
-
183
- $ model ->exists = true ;
167
+ // Update timestamps.
168
+ $ this ->updateTimestamps ($ model );
184
169
185
170
// Push the document to the database.
186
171
$ result = $ this ->query ->push ($ this ->localKey , $ model ->getAttributes (), true );
187
172
188
- // Get existing embedded documents.
189
173
$ documents = $ this ->getEmbeddedRecords ();
190
174
191
175
// Add the document to the parent model.
192
176
$ documents [] = $ model ->getAttributes ();
193
177
194
178
$ this ->setEmbeddedRecords ($ documents );
195
179
180
+ // Mark the model as existing.
181
+ $ model ->exists = true ;
182
+
196
183
return $ result ? $ model : false ;
197
184
}
198
185
@@ -205,15 +192,10 @@ protected function performInsert(Model $model)
205
192
protected function performUpdate (Model $ model )
206
193
{
207
194
// Update timestamps.
208
- if ($ model ->usesTimestamps ())
209
- {
210
- $ time = $ model ->freshTimestamp ();
211
-
212
- $ model ->setUpdatedAt ($ time );
213
- }
195
+ $ this ->updateTimestamps ($ model );
214
196
215
- // Convert the id to MongoId if necessary .
216
- $ id = $ this ->query -> getQuery ()-> convertKey ($ model ->getKey ());
197
+ // Get the correct foreign key value .
198
+ $ id = $ this ->getForeignKeyValue ($ model ->getKey ());
217
199
218
200
// Update document in database.
219
201
$ result = $ this ->query ->where ($ this ->localKey . '. ' . $ model ->getKeyName (), $ id )
@@ -315,11 +297,7 @@ public function destroy($ids = array())
315
297
// Pull the documents from the database.
316
298
foreach ($ ids as $ id )
317
299
{
318
- // Convert the id to MongoId if necessary.
319
- $ id = $ this ->query ->getQuery ()->convertKey ($ id );
320
-
321
- $ this ->query ->pull ($ this ->localKey , array ($ primaryKey => $ id ));
322
-
300
+ $ this ->query ->pull ($ this ->localKey , array ($ primaryKey => $ this ->getForeignKeyValue ($ id )));
323
301
$ count ++;
324
302
}
325
303
@@ -373,9 +351,12 @@ protected function toCollection(array $results = array())
373
351
$ models = array ();
374
352
375
353
// Wrap documents in model objects.
376
- foreach ($ results as $ result )
354
+ foreach ($ results as $ model )
377
355
{
378
- $ model = $ this ->related ->newFromBuilder ($ result );
356
+ if ( ! $ model instanceof Model)
357
+ {
358
+ $ model = $ this ->related ->newFromBuilder ($ model );
359
+ }
379
360
380
361
// Attatch the parent relation to the embedded model.
381
362
$ model ->setRelation ($ this ->foreignKey , $ this ->parent );
@@ -391,10 +372,15 @@ protected function toCollection(array $results = array())
391
372
*
392
373
* @return array
393
374
*/
394
- protected function getEmbeddedRecords ()
375
+ protected function getEmbeddedRecords (Model $ model = null )
395
376
{
377
+ if (is_null ($ model ))
378
+ {
379
+ $ model = $ this ->parent ;
380
+ }
381
+
396
382
// Get raw attributes to skip relations and accessors.
397
- $ attributes = $ this -> parent ->getAttributes ();
383
+ $ attributes = $ model ->getAttributes ();
398
384
399
385
return isset ($ attributes [$ this ->localKey ]) ? $ attributes [$ this ->localKey ] : array ();
400
386
}
@@ -417,4 +403,36 @@ protected function setEmbeddedRecords(array $models)
417
403
$ this ->parent ->setRelation ($ this ->relation , $ this ->getResults ());
418
404
}
419
405
406
+ /**
407
+ * Update the creation and update timestamps.
408
+ *
409
+ * @return void
410
+ */
411
+ protected function updateTimestamps (Model $ model )
412
+ {
413
+ $ time = $ model ->freshTimestamp ();
414
+
415
+ if ( ! $ model ->isDirty (Model::UPDATED_AT ))
416
+ {
417
+ $ model ->setUpdatedAt ($ time );
418
+ }
419
+
420
+ if ( ! $ model ->exists && ! $ model ->isDirty (Model::CREATED_AT ))
421
+ {
422
+ $ model ->setCreatedAt ($ time );
423
+ }
424
+ }
425
+
426
+ /**
427
+ * Get the foreign key value for the relation.
428
+ *
429
+ * @param mixed $id
430
+ * @return mixed
431
+ */
432
+ protected function getForeignKeyValue ($ id )
433
+ {
434
+ // Convert the id to MongoId if necessary.
435
+ return $ this ->getBaseQuery ()->convertKey ($ id );
436
+ }
437
+
420
438
}
0 commit comments