Skip to content

Commit a1d9410

Browse files
committed
Fixed eager loading, issue #138
1 parent 48ac180 commit a1d9410

File tree

3 files changed

+83
-65
lines changed

3 files changed

+83
-65
lines changed

src/Jenssegers/Eloquent/Model.php

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use Illuminate\Database\Eloquent\Relations\Relation;
88
use Jenssegers\Mongodb\Relations\BelongsTo;
99
use Jenssegers\Mongodb\Relations\BelongsToMany;
10-
use Jenssegers\Mongodb\Relations\EmbedsMany;
1110
use Jenssegers\Mongodb\Query\Builder as QueryBuilder;
1211

1312
abstract class Model extends \Illuminate\Database\Eloquent\Model {
@@ -221,40 +220,6 @@ public function belongsToMany($related, $collection = null, $foreignKey = null,
221220
return new BelongsToMany($query, $this, $collection, $foreignKey, $otherKey, $relation);
222221
}
223222

224-
/**
225-
* Define an embedded one-to-many relationship.
226-
*
227-
* @param string $related
228-
* @param string $collection
229-
* @return \Illuminate\Database\Eloquent\Relations\EmbedsMany
230-
*/
231-
protected function embedsMany($related, $localKey = null, $foreignKey = null, $relation = null)
232-
{
233-
if (is_null($localKey))
234-
{
235-
$localKey = snake_case(str_plural($related)) . '_ids';
236-
}
237-
238-
if (is_null($foreignKey))
239-
{
240-
$foreignKey = snake_case(class_basename($this));
241-
}
242-
243-
// If no relation name was given, we will use this debug backtrace to extract
244-
// the calling method's name and use that as the relationship name as most
245-
// of the time this will be what we desire to use for the relatinoships.
246-
if (is_null($relation))
247-
{
248-
list(, $caller) = debug_backtrace(false);
249-
250-
$relation = $caller['function'];
251-
}
252-
253-
$query = $this->newQuery();
254-
255-
return new EmbedsMany($query, $this, $localKey, $foreignKey, $relation);
256-
}
257-
258223
/**
259224
* Get a new query builder instance for the connection.
260225
*

src/Jenssegers/Mongodb/Model.php

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
<?php namespace Jenssegers\Mongodb;
22

33
use Illuminate\Database\Eloquent\Collection;
4-
use Illuminate\Database\Eloquent\Relations\HasOne;
5-
use Illuminate\Database\Eloquent\Relations\HasMany;
6-
74
use Jenssegers\Mongodb\DatabaseManager as Resolver;
85
use Jenssegers\Mongodb\Eloquent\Builder;
96
use Jenssegers\Mongodb\Query\Builder as QueryBuilder;
10-
use Jenssegers\Mongodb\Relations\BelongsTo;
11-
use Jenssegers\Mongodb\Relations\BelongsToMany;
7+
use Jenssegers\Mongodb\Relations\EmbedsMany;
128

139
use Carbon\Carbon;
1410
use DateTime;
@@ -52,6 +48,40 @@ public function getIdAttribute($value)
5248
if (array_key_exists($this->getKeyName(), $this->attributes)) return $this->attributes[$this->getKeyName()];
5349
}
5450

51+
/**
52+
* Define an embedded one-to-many relationship.
53+
*
54+
* @param string $related
55+
* @param string $collection
56+
* @return \Illuminate\Database\Eloquent\Relations\EmbedsMany
57+
*/
58+
protected function embedsMany($related, $localKey = null, $foreignKey = null, $relation = null)
59+
{
60+
if (is_null($localKey))
61+
{
62+
$localKey = snake_case(str_plural($related)) . '_ids';
63+
}
64+
65+
if (is_null($foreignKey))
66+
{
67+
$foreignKey = snake_case(class_basename($this));
68+
}
69+
70+
// If no relation name was given, we will use this debug backtrace to extract
71+
// the calling method's name and use that as the relationship name as most
72+
// of the time this will be what we desire to use for the relatinoships.
73+
if (is_null($relation))
74+
{
75+
list(, $caller) = debug_backtrace(false);
76+
77+
$relation = $caller['function'];
78+
}
79+
80+
$query = $this->newQuery();
81+
82+
return new EmbedsMany($query, $this, $localKey, $foreignKey, $relation);
83+
}
84+
5585
/**
5686
* Convert a DateTime to a storable MongoDate object.
5787
*

src/Jenssegers/Mongodb/Relations/EmbedsMany.php

Lines changed: 48 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)