|
1 | 1 | <?php namespace Jenssegers\Eloquent; |
2 | 2 |
|
3 | | -use LogicException; |
4 | 3 | use Illuminate\Database\Eloquent\Relations\HasOne; |
5 | 4 | use Illuminate\Database\Eloquent\Relations\HasMany; |
6 | 5 | use Illuminate\Database\Eloquent\Relations\MorphOne; |
|
9 | 8 | use Jenssegers\Mongodb\Relations\BelongsTo; |
10 | 9 | use Jenssegers\Mongodb\Relations\BelongsToMany; |
11 | 10 | use Jenssegers\Mongodb\Relations\EmbedsMany; |
12 | | -use Jenssegers\Mongodb\Relations\EmbeddedRelation; |
13 | 11 | use Jenssegers\Mongodb\Query\Builder as QueryBuilder; |
14 | 12 |
|
15 | 13 | abstract class Model extends \Illuminate\Database\Eloquent\Model { |
@@ -230,36 +228,31 @@ public function belongsToMany($related, $collection = null, $foreignKey = null, |
230 | 228 | * @param string $collection |
231 | 229 | * @return \Illuminate\Database\Eloquent\Relations\EmbedsMany |
232 | 230 | */ |
233 | | - protected function embedsMany($related, $collection = null) |
| 231 | + protected function embedsMany($related, $localKey = null, $foreignKey = null, $relation = null) |
234 | 232 | { |
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)) |
238 | 234 | { |
239 | | - $collection = str_plural(snake_case($related)) . '_ids'; |
| 235 | + $localKey = snake_case(str_plural($related)) . '_ids'; |
240 | 236 | } |
241 | 237 |
|
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 | + } |
255 | 242 |
|
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)) |
257 | 247 | { |
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']; |
260 | 251 | } |
261 | 252 |
|
262 | | - return $this->relations[$key] = $relations->getResults(); |
| 253 | + $query = $this->newQuery(); |
| 254 | + |
| 255 | + return new EmbedsMany($query, $this, $localKey, $foreignKey, $relation); |
263 | 256 | } |
264 | 257 |
|
265 | 258 | /** |
|
0 commit comments