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