Skip to content

Commit 177ab04

Browse files
committed
Tweaking embedsMany, check issue #138
1 parent 828a490 commit 177ab04

File tree

5 files changed

+343
-170
lines changed

5 files changed

+343
-170
lines changed

README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,43 @@ The belongsToMany relation will not use a pivot "table", but will push id's to a
359359

360360
Other relations are not yet supported, but may be added in the future. Read more about these relations on http://four.laravel.com/docs/eloquent#relationships
361361

362+
### EmbedsMany Relations
363+
364+
If you want to embed documents, rather than referencing them, you can use the `embedsMany` relation:
365+
366+
use Jenssegers\Mongodb\Model as Eloquent;
367+
368+
class User extends Eloquent {
369+
370+
public function books()
371+
{
372+
return $this->embedsMany('Book');
373+
}
374+
375+
}
376+
377+
Now we can access the post's comments through the dynamic property:
378+
379+
$books = User::first()->books;
380+
381+
Inserting and updating embedded documents works just like the `belongsTo` relation:
382+
383+
$book = new Book(array('title' => 'A Game of Thrones'));
384+
385+
$user = User::first();
386+
387+
$book = $user->books()->save($book);
388+
389+
You can remove an embedded document by using the `destroy()` method:
390+
391+
$book = $user->books()->first();
392+
393+
$user->books()->destroy($book->_id);
394+
395+
Again, you may override the conventional local key by passing a second argument to the embedsMany method:
396+
397+
return $this->embedsMany('Book', 'local_key');
398+
362399
### MySQL Relations
363400

364401
If you're using a hybrid MongoDB and SQL setup, you're in luck! The model will automatically return a MongoDB- or SQL-relation based on the type of the related model. Of course, if you want this functionality to work both ways, your SQL-models will need to extend `Jenssegers\Eloquent\Model`. Note that this functionality only works for hasOne, hasMany and belongsTo relations.

src/Jenssegers/Eloquent/Model.php

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?php namespace Jenssegers\Eloquent;
22

3-
use LogicException;
43
use Illuminate\Database\Eloquent\Relations\HasOne;
54
use Illuminate\Database\Eloquent\Relations\HasMany;
65
use Illuminate\Database\Eloquent\Relations\MorphOne;
@@ -9,7 +8,6 @@
98
use Jenssegers\Mongodb\Relations\BelongsTo;
109
use Jenssegers\Mongodb\Relations\BelongsToMany;
1110
use Jenssegers\Mongodb\Relations\EmbedsMany;
12-
use Jenssegers\Mongodb\Relations\EmbeddedRelation;
1311
use Jenssegers\Mongodb\Query\Builder as QueryBuilder;
1412

1513
abstract class Model extends \Illuminate\Database\Eloquent\Model {
@@ -230,36 +228,31 @@ public function belongsToMany($related, $collection = null, $foreignKey = null,
230228
* @param string $collection
231229
* @return \Illuminate\Database\Eloquent\Relations\EmbedsMany
232230
*/
233-
protected function embedsMany($related, $collection = null)
231+
protected function embedsMany($related, $localKey = null, $foreignKey = null, $relation = null)
234232
{
235-
// If no collection name was provided, we assume that the standard is the
236-
// related model camel_cased, pluralized and suffixed with '_ids'
237-
if (is_null($collection))
233+
if (is_null($localKey))
238234
{
239-
$collection = str_plural(snake_case($related)) . '_ids';
235+
$localKey = snake_case(str_plural($related)) . '_ids';
240236
}
241237

242-
return new EmbedsMany($this, $related, $collection);
243-
}
244-
245-
/**
246-
* Get a relationship value from a method.
247-
*
248-
* @param string $key
249-
* @param string $camelKey
250-
* @return mixed
251-
*/
252-
protected function getRelationshipFromMethod($key, $camelKey)
253-
{
254-
$relations = $this->$camelKey();
238+
if (is_null($foreignKey))
239+
{
240+
$foreignKey = snake_case(class_basename($this));
241+
}
255242

256-
if ( ! $relations instanceof Relation and ! $relations instanceof EmbeddedRelation)
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))
257247
{
258-
throw new LogicException('Relationship method must return an object of type '
259-
. 'Illuminate\Database\Eloquent\Relations\Relation or Jenssegers\Mongodb\Relations\EmbeddedRelation');
248+
list(, $caller) = debug_backtrace(false);
249+
250+
$relation = $caller['function'];
260251
}
261252

262-
return $this->relations[$key] = $relations->getResults();
253+
$query = $this->newQuery();
254+
255+
return new EmbedsMany($query, $this, $localKey, $foreignKey, $relation);
263256
}
264257

265258
/**

src/Jenssegers/Mongodb/Relations/EmbeddedRelation.php

Lines changed: 0 additions & 42 deletions
This file was deleted.

0 commit comments

Comments
 (0)