|
5 | 5 | use Jenssegers\Mongodb\Eloquent\Builder;
|
6 | 6 | use Jenssegers\Mongodb\Query\Builder as QueryBuilder;
|
7 | 7 | use Jenssegers\Mongodb\Relations\EmbedsMany;
|
| 8 | +use Jenssegers\Mongodb\Relations\EmbedsOne; |
8 | 9 |
|
9 | 10 | use Carbon\Carbon;
|
10 | 11 | use DateTime;
|
@@ -84,6 +85,42 @@ protected function embedsMany($related, $localKey = null, $foreignKey = null, $r
|
84 | 85 | return new EmbedsMany($query, $this, $instance, $localKey, $foreignKey, $relation);
|
85 | 86 | }
|
86 | 87 |
|
| 88 | + /** |
| 89 | + * Define an embedded one-to-many relationship. |
| 90 | + * |
| 91 | + * @param string $related |
| 92 | + * @param string $collection |
| 93 | + * @return \Illuminate\Database\Eloquent\Relations\EmbedsMany |
| 94 | + */ |
| 95 | + protected function embedsOne($related, $localKey = null, $foreignKey = null, $relation = null) |
| 96 | + { |
| 97 | + // If no relation name was given, we will use this debug backtrace to extract |
| 98 | + // the calling method's name and use that as the relationship name as most |
| 99 | + // of the time this will be what we desire to use for the relatinoships. |
| 100 | + if (is_null($relation)) |
| 101 | + { |
| 102 | + list(, $caller) = debug_backtrace(false); |
| 103 | + |
| 104 | + $relation = $caller['function']; |
| 105 | + } |
| 106 | + |
| 107 | + if (is_null($localKey)) |
| 108 | + { |
| 109 | + $localKey = '_' . $relation; |
| 110 | + } |
| 111 | + |
| 112 | + if (is_null($foreignKey)) |
| 113 | + { |
| 114 | + $foreignKey = snake_case(class_basename($this)); |
| 115 | + } |
| 116 | + |
| 117 | + $query = $this->newQuery(); |
| 118 | + |
| 119 | + $instance = new $related; |
| 120 | + |
| 121 | + return new EmbedsOne($query, $this, $instance, $localKey, $foreignKey, $relation); |
| 122 | + } |
| 123 | + |
87 | 124 | /**
|
88 | 125 | * Convert a DateTime to a storable MongoDate object.
|
89 | 126 | *
|
|
0 commit comments