7
7
use Jenssegers \Mongodb \DatabaseManager as Resolver ;
8
8
use Jenssegers \Mongodb \Builder as QueryBuilder ;
9
9
use Jenssegers \Mongodb \Relations \BelongsTo ;
10
+ use Jenssegers \Mongodb \Relations \BelongsToMany ;
10
11
11
- use Carbon \Carbon ;
12
12
use DateTime ;
13
13
use MongoId ;
14
14
use MongoDate ;
@@ -67,25 +67,19 @@ public function fromDateTime($value)
67
67
*/
68
68
protected function asDateTime ($ value )
69
69
{
70
- // Convert timestamp
71
- if (is_numeric ($ value ))
72
- {
73
- return Carbon::createFromTimestamp ($ value );
74
- }
75
-
76
- // Convert string
77
- if (is_string ($ value ))
70
+ // Convert MongoDate to timestamp
71
+ if ($ value instanceof MongoDate)
78
72
{
79
- return new Carbon ( $ value) ;
73
+ $ value = $ value-> sec ;
80
74
}
81
75
82
- // Convert MongoDate
83
- if ($ value instanceof MongoDate )
76
+ // Convert timestamp to string for DateTime
77
+ if (is_int ( $ value) )
84
78
{
85
- return Carbon:: createFromTimestamp ( $ value-> sec ) ;
79
+ $ value = " @ $ value" ;
86
80
}
87
81
88
- return Carbon:: instance ($ value );
82
+ return new DateTime ($ value );
89
83
}
90
84
91
85
/**
@@ -121,83 +115,104 @@ public function getTable()
121
115
}
122
116
123
117
/**
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 )
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 )
132
125
{
133
126
$ foreignKey = $ foreignKey ?: $ this ->getForeignKey ();
134
127
135
128
$ instance = new $ related ;
136
129
137
- $ localKey = $ localKey ?: $ this ->getKeyName ();
138
-
139
- return new HasOne ($ instance ->newQuery (), $ this , $ foreignKey , $ localKey );
130
+ return new HasOne ($ instance ->newQuery (), $ this , $ foreignKey );
140
131
}
141
132
142
133
/**
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 )
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 )
151
141
{
152
142
$ foreignKey = $ foreignKey ?: $ this ->getForeignKey ();
153
143
154
144
$ instance = new $ related ;
155
145
156
- $ localKey = $ localKey ?: $ this ->getKeyName ();
157
-
158
- return new HasMany ($ instance ->newQuery (), $ this , $ foreignKey , $ localKey );
146
+ return new HasMany ($ instance ->newQuery (), $ this , $ foreignKey );
159
147
}
160
148
161
149
/**
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 )
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 )
171
157
{
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
+ list (, $ caller ) = debug_backtrace (false );
181
159
182
160
// If no foreign key was supplied, we can use a backtrace to guess the proper
183
161
// foreign key name by using the name of the relationship function, which
184
162
// when combined with an "_id" should conventionally match the columns.
163
+ $ relation = $ caller ['function ' ];
164
+
185
165
if (is_null ($ foreignKey ))
186
166
{
187
167
$ foreignKey = snake_case ($ relation ).'_id ' ;
188
168
}
189
169
190
- $ instance = new $ related ;
191
-
192
170
// Once we have the foreign key names, we'll just create a new Eloquent query
193
171
// for the related models and returns the relationship instance which will
194
172
// actually be responsible for retrieving and hydrating every relations.
195
- $ query = $ instance -> newQuery () ;
173
+ $ instance = new $ related ;
196
174
197
- $ otherKey = $ otherKey ?: $ instance ->getKeyName ();
175
+ $ query = $ instance ->newQuery ();
198
176
199
- return new BelongsTo ($ query , $ this , $ foreignKey , $ otherKey , $ relation );
177
+ return new BelongsTo ($ query , $ this , $ foreignKey , $ relation );
200
178
}
179
+
180
+ /**
181
+ * Define a many-to-many relationship.
182
+ *
183
+ * @param string $related
184
+ * @param string $table
185
+ * @param string $foreignKey
186
+ * @param string $otherKey
187
+ * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
188
+ */
189
+ public function belongsToMany ($ related , $ collection = null , $ foreignKey = null , $ otherKey = null )
190
+ {
191
+ $ caller = $ this ->getBelongsToManyCaller ();
192
+
193
+ // First, we'll need to determine the foreign key and "other key" for the
194
+ // relationship. Once we have determined the keys we'll make the query
195
+ // instances as well as the relationship instances we need for this.
196
+ $ foreignKey = $ foreignKey ?: $ this ->getForeignKey () . 's ' ;
197
+
198
+ $ instance = new $ related ;
199
+
200
+ $ otherKey = $ otherKey ?: $ instance ->getForeignKey () . 's ' ;
201
+ // If no table name was provided, we can guess it by concatenating the two
202
+ // models using underscores in alphabetical order. The two model names
203
+ // are transformed to snake case from their default CamelCase also.
204
+ if (is_null ($ collection ))
205
+ {
206
+ $ collection = snake_case (str_plural (class_basename ($ related )));
207
+ }
208
+
209
+ // Now we're ready to create a new query builder for the related model and
210
+ // the relationship instances for the relation. The relations will set
211
+ // appropriate query constraint and entirely manages the hydrations.
212
+ $ query = $ instance ->newQuery ();
213
+
214
+ return new BelongsToMany ($ query , $ this , $ collection , $ foreignKey , $ otherKey , $ caller ['function ' ]);
215
+ }
201
216
202
217
/**
203
218
* Get a new query builder instance for the connection.
@@ -251,7 +266,7 @@ public function dropColumn($columns)
251
266
{
252
267
$ this ->__unset ($ column );
253
268
}
254
-
269
+
255
270
// Perform unset only on current document
256
271
return $ query = $ this ->newQuery ()->where ($ this ->getKeyName (), $ this ->getKey ())->unset ($ columns );
257
272
}
@@ -265,7 +280,6 @@ public function dropColumn($columns)
265
280
*/
266
281
public function __call ($ method , $ parameters )
267
282
{
268
- // Unset method
269
283
if ($ method == 'unset ' )
270
284
{
271
285
return call_user_func_array (array ($ this , 'dropColumn ' ), $ parameters );
@@ -274,4 +288,4 @@ public function __call($method, $parameters)
274
288
return parent ::__call ($ method , $ parameters );
275
289
}
276
290
277
- }
291
+ }
0 commit comments