|
1 | 1 | <?php namespace Jenssegers\Mongodb; |
2 | 2 |
|
3 | 3 | use Illuminate\Database\Eloquent\Collection; |
| 4 | +use Illuminate\Database\Eloquent\Relations\HasOne; |
| 5 | +use Illuminate\Database\Eloquent\Relations\HasMany; |
| 6 | + |
4 | 7 | use Jenssegers\Mongodb\DatabaseManager as Resolver; |
5 | 8 | use Jenssegers\Mongodb\Builder as QueryBuilder; |
| 9 | +use Jenssegers\Mongodb\Relations\BelongsTo; |
6 | 10 |
|
7 | 11 | use DateTime; |
8 | 12 | use MongoId; |
@@ -107,6 +111,69 @@ public function getTable() |
107 | 111 | return parent::getTable(); |
108 | 112 | } |
109 | 113 |
|
| 114 | + /** |
| 115 | + * Define a one-to-one relationship. |
| 116 | + * |
| 117 | + * @param string $related |
| 118 | + * @param string $foreignKey |
| 119 | + * @return \Illuminate\Database\Eloquent\Relations\HasOne |
| 120 | + */ |
| 121 | + public function hasOne($related, $foreignKey = null) |
| 122 | + { |
| 123 | + $foreignKey = $foreignKey ?: $this->getForeignKey(); |
| 124 | + |
| 125 | + $instance = new $related; |
| 126 | + |
| 127 | + return new HasOne($instance->newQuery(), $this, $foreignKey); |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * Define a one-to-many relationship. |
| 132 | + * |
| 133 | + * @param string $related |
| 134 | + * @param string $foreignKey |
| 135 | + * @return \Illuminate\Database\Eloquent\Relations\HasMany |
| 136 | + */ |
| 137 | + public function hasMany($related, $foreignKey = null) |
| 138 | + { |
| 139 | + $foreignKey = $foreignKey ?: $this->getForeignKey(); |
| 140 | + |
| 141 | + $instance = new $related; |
| 142 | + |
| 143 | + return new HasMany($instance->newQuery(), $this, $foreignKey); |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * Define an inverse one-to-one or many relationship. |
| 148 | + * |
| 149 | + * @param string $related |
| 150 | + * @param string $foreignKey |
| 151 | + * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
| 152 | + */ |
| 153 | + public function belongsTo($related, $foreignKey = null) |
| 154 | + { |
| 155 | + list(, $caller) = debug_backtrace(false); |
| 156 | + |
| 157 | + // If no foreign key was supplied, we can use a backtrace to guess the proper |
| 158 | + // foreign key name by using the name of the relationship function, which |
| 159 | + // when combined with an "_id" should conventionally match the columns. |
| 160 | + $relation = $caller['function']; |
| 161 | + |
| 162 | + if (is_null($foreignKey)) |
| 163 | + { |
| 164 | + $foreignKey = snake_case($relation).'_id'; |
| 165 | + } |
| 166 | + |
| 167 | + // Once we have the foreign key names, we'll just create a new Eloquent query |
| 168 | + // for the related models and returns the relationship instance which will |
| 169 | + // actually be responsible for retrieving and hydrating every relations. |
| 170 | + $instance = new $related; |
| 171 | + |
| 172 | + $query = $instance->newQuery(); |
| 173 | + |
| 174 | + return new BelongsTo($query, $this, $foreignKey, $relation); |
| 175 | + } |
| 176 | + |
110 | 177 | /** |
111 | 178 | * Get a new query builder instance for the connection. |
112 | 179 | * |
|
0 commit comments