99use Jenssegers \Mongodb \Relations \BelongsTo ;
1010use Jenssegers \Mongodb \Relations \BelongsToMany ;
1111
12+ use Carbon \Carbon ;
1213use DateTime ;
1314use MongoId ;
1415use MongoDate ;
@@ -67,19 +68,25 @@ public function fromDateTime($value)
6768 */
6869 protected function asDateTime ($ value )
6970 {
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 ))
7279 {
73- $ value = $ value-> sec ;
80+ return new Carbon ( $ value) ;
7481 }
7582
76- // Convert timestamp to string for DateTime
77- if (is_int ( $ value) )
83+ // Convert MongoDate
84+ if ($ value instanceof MongoDate )
7885 {
79- $ value = " @ $ value" ;
86+ return Carbon:: createFromTimestamp ( $ value-> sec ) ;
8087 }
8188
82- return new DateTime ($ value );
89+ return Carbon:: instance ($ value );
8390 }
8491
8592 /**
@@ -115,68 +122,84 @@ public function getTable()
115122 }
116123
117124 /**
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 )
125133 {
126134 $ foreignKey = $ foreignKey ?: $ this ->getForeignKey ();
127135
128136 $ instance = new $ related ;
129137
130- return new HasOne ($ instance ->newQuery (), $ this , $ foreignKey );
138+ $ localKey = $ localKey ?: $ this ->getKeyName ();
139+
140+ return new HasOne ($ instance ->newQuery (), $ this , $ foreignKey , $ localKey );
131141 }
132142
133143 /**
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 )
141152 {
142153 $ foreignKey = $ foreignKey ?: $ this ->getForeignKey ();
143154
144155 $ instance = new $ related ;
145156
146- return new HasMany ($ instance ->newQuery (), $ this , $ foreignKey );
157+ $ localKey = $ localKey ?: $ this ->getKeyName ();
158+
159+ return new HasMany ($ instance ->newQuery (), $ this , $ foreignKey , $ localKey );
147160 }
148161
149162 /**
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 )
157172 {
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+ }
159182
160183 // If no foreign key was supplied, we can use a backtrace to guess the proper
161184 // foreign key name by using the name of the relationship function, which
162185 // when combined with an "_id" should conventionally match the columns.
163- $ relation = $ caller ['function ' ];
164-
165186 if (is_null ($ foreignKey ))
166187 {
167188 $ foreignKey = snake_case ($ relation ).'_id ' ;
168189 }
169190
191+ $ instance = new $ related ;
192+
170193 // Once we have the foreign key names, we'll just create a new Eloquent query
171194 // for the related models and returns the relationship instance which will
172195 // actually be responsible for retrieving and hydrating every relations.
173- $ instance = new $ related ;
174-
175196 $ query = $ instance ->newQuery ();
176197
177- return new BelongsTo ($ query , $ this , $ foreignKey , $ relation );
198+ $ otherKey = $ otherKey ?: $ instance ->getKeyName ();
199+
200+ return new BelongsTo ($ query , $ this , $ foreignKey , $ otherKey , $ relation );
178201 }
179-
202+
180203 /**
181204 * Define a many-to-many relationship.
182205 *
@@ -266,7 +289,7 @@ public function dropColumn($columns)
266289 {
267290 $ this ->__unset ($ column );
268291 }
269-
292+
270293 // Perform unset only on current document
271294 return $ query = $ this ->newQuery ()->where ($ this ->getKeyName (), $ this ->getKey ())->unset ($ columns );
272295 }
@@ -280,6 +303,7 @@ public function dropColumn($columns)
280303 */
281304 public function __call ($ method , $ parameters )
282305 {
306+ // Unset method
283307 if ($ method == 'unset ' )
284308 {
285309 return call_user_func_array (array ($ this , 'dropColumn ' ), $ parameters );
0 commit comments