|
8 | 8 | use Jenssegers\Mongodb\Builder as QueryBuilder; |
9 | 9 | use Jenssegers\Mongodb\Relations\BelongsTo; |
10 | 10 |
|
| 11 | +use Carbon\Carbon; |
11 | 12 | use DateTime; |
12 | 13 | use MongoId; |
13 | 14 | use MongoDate; |
@@ -66,19 +67,25 @@ public function fromDateTime($value) |
66 | 67 | */ |
67 | 68 | protected function asDateTime($value) |
68 | 69 | { |
69 | | - // Convert MongoDate to timestamp |
70 | | - if ($value instanceof MongoDate) |
| 70 | + // Convert timestamp |
| 71 | + if (is_numeric($value)) |
| 72 | + { |
| 73 | + return Carbon::createFromTimestamp($value); |
| 74 | + } |
| 75 | + |
| 76 | + // Convert string |
| 77 | + if (is_string($value)) |
71 | 78 | { |
72 | | - $value = $value->sec; |
| 79 | + return new Carbon($value); |
73 | 80 | } |
74 | 81 |
|
75 | | - // Convert timestamp to string for DateTime |
76 | | - if (is_int($value)) |
| 82 | + // Convert MongoDate |
| 83 | + if ($value instanceof MongoDate) |
77 | 84 | { |
78 | | - $value = "@$value"; |
| 85 | + return Carbon::createFromTimestamp($value->sec); |
79 | 86 | } |
80 | 87 |
|
81 | | - return new DateTime($value); |
| 88 | + return Carbon::instance($value); |
82 | 89 | } |
83 | 90 |
|
84 | 91 | /** |
@@ -114,66 +121,82 @@ public function getTable() |
114 | 121 | } |
115 | 122 |
|
116 | 123 | /** |
117 | | - * Define a one-to-one relationship. |
118 | | - * |
119 | | - * @param string $related |
120 | | - * @param string $foreignKey |
121 | | - * @return \Illuminate\Database\Eloquent\Relations\HasOne |
122 | | - */ |
123 | | - public function hasOne($related, $foreignKey = null) |
| 124 | + * Define a one-to-one relationship. |
| 125 | + * |
| 126 | + * @param string $related |
| 127 | + * @param string $foreignKey |
| 128 | + * @param string $localKey |
| 129 | + * @return \Illuminate\Database\Eloquent\Relations\HasOne |
| 130 | + */ |
| 131 | + public function hasOne($related, $foreignKey = null, $localKey = null) |
124 | 132 | { |
125 | 133 | $foreignKey = $foreignKey ?: $this->getForeignKey(); |
126 | 134 |
|
127 | 135 | $instance = new $related; |
128 | 136 |
|
129 | | - return new HasOne($instance->newQuery(), $this, $foreignKey); |
| 137 | + $localKey = $localKey ?: $this->getKeyName(); |
| 138 | + |
| 139 | + return new HasOne($instance->newQuery(), $this, $foreignKey, $localKey); |
130 | 140 | } |
131 | 141 |
|
132 | 142 | /** |
133 | | - * Define a one-to-many relationship. |
134 | | - * |
135 | | - * @param string $related |
136 | | - * @param string $foreignKey |
137 | | - * @return \Illuminate\Database\Eloquent\Relations\HasMany |
138 | | - */ |
139 | | - public function hasMany($related, $foreignKey = null) |
| 143 | + * Define a one-to-many relationship. |
| 144 | + * |
| 145 | + * @param string $related |
| 146 | + * @param string $foreignKey |
| 147 | + * @param string $localKey |
| 148 | + * @return \Illuminate\Database\Eloquent\Relations\HasMany |
| 149 | + */ |
| 150 | + public function hasMany($related, $foreignKey = null, $localKey = null) |
140 | 151 | { |
141 | 152 | $foreignKey = $foreignKey ?: $this->getForeignKey(); |
142 | 153 |
|
143 | 154 | $instance = new $related; |
144 | 155 |
|
145 | | - return new HasMany($instance->newQuery(), $this, $foreignKey); |
| 156 | + $localKey = $localKey ?: $this->getKeyName(); |
| 157 | + |
| 158 | + return new HasMany($instance->newQuery(), $this, $foreignKey, $localKey); |
146 | 159 | } |
147 | 160 |
|
148 | 161 | /** |
149 | | - * Define an inverse one-to-one or many relationship. |
150 | | - * |
151 | | - * @param string $related |
152 | | - * @param string $foreignKey |
153 | | - * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
154 | | - */ |
155 | | - public function belongsTo($related, $foreignKey = null) |
| 162 | + * Define an inverse one-to-one or many relationship. |
| 163 | + * |
| 164 | + * @param string $related |
| 165 | + * @param string $foreignKey |
| 166 | + * @param string $otherKey |
| 167 | + * @param string $relation |
| 168 | + * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
| 169 | + */ |
| 170 | + public function belongsTo($related, $foreignKey = null, $otherKey = null, $relation = null) |
156 | 171 | { |
157 | | - list(, $caller) = debug_backtrace(false); |
| 172 | + // If no relation name was given, we will use this debug backtrace to extract |
| 173 | + // the calling method's name and use that as the relationship name as most |
| 174 | + // of the time this will be what we desire to use for the relatinoships. |
| 175 | + if (is_null($relation)) |
| 176 | + { |
| 177 | + list(, $caller) = debug_backtrace(false); |
| 178 | + |
| 179 | + $relation = $caller['function']; |
| 180 | + } |
158 | 181 |
|
159 | 182 | // If no foreign key was supplied, we can use a backtrace to guess the proper |
160 | 183 | // foreign key name by using the name of the relationship function, which |
161 | 184 | // when combined with an "_id" should conventionally match the columns. |
162 | | - $relation = $caller['function']; |
163 | | - |
164 | 185 | if (is_null($foreignKey)) |
165 | 186 | { |
166 | 187 | $foreignKey = snake_case($relation).'_id'; |
167 | 188 | } |
168 | 189 |
|
| 190 | + $instance = new $related; |
| 191 | + |
169 | 192 | // Once we have the foreign key names, we'll just create a new Eloquent query |
170 | 193 | // for the related models and returns the relationship instance which will |
171 | 194 | // actually be responsible for retrieving and hydrating every relations. |
172 | | - $instance = new $related; |
173 | | - |
174 | 195 | $query = $instance->newQuery(); |
175 | 196 |
|
176 | | - return new BelongsTo($query, $this, $foreignKey, $relation); |
| 197 | + $otherKey = $otherKey ?: $instance->getKeyName(); |
| 198 | + |
| 199 | + return new BelongsTo($query, $this, $foreignKey, $otherKey, $relation); |
177 | 200 | } |
178 | 201 |
|
179 | 202 | /** |
|
0 commit comments