1010use Jenssegers \Mongodb \Relations \EmbedsMany ;
1111use Jenssegers \Mongodb \Relations \EmbedsOne ;
1212use Jenssegers \Mongodb \Relations \EmbedsOneOrMany ;
13+ use MongoDB \BSON \ObjectID ;
14+ use MongoDB \BSON \UTCDateTime ;
1315use ReflectionMethod ;
14- use MongoDB ;
16+
1517abstract class Model extends BaseModel {
1618
1719 use HybridRelations;
@@ -52,8 +54,8 @@ public function getIdAttribute($value)
5254 $ value = $ this ->attributes ['_id ' ];
5355 }
5456
55- // Convert MongoDB\BSON\ ObjectID's to string.
56- if ($ value instanceof MongoDB \ BSON \ ObjectID)
57+ // Convert ObjectID to string.
58+ if ($ value instanceof ObjectID)
5759 {
5860 return (string ) $ value ;
5961 }
@@ -148,15 +150,15 @@ protected function embedsOne($related, $localKey = null, $foreignKey = null, $re
148150 }
149151
150152 /**
151- * Convert a DateTime to a storable MongoDB\BSON\ UTCDateTime object.
153+ * Convert a DateTime to a storable UTCDateTime object.
152154 *
153155 * @param DateTime|int $value
154- * @return MongoDB\BSON\ UTCDateTime
156+ * @return UTCDateTime
155157 */
156158 public function fromDateTime ($ value )
157159 {
158- // If the value is already a MongoDB\BSON\ UTCDateTime instance, we don't need to parse it.
159- if ($ value instanceof MongoDB \ BSON \ UTCDateTime)
160+ // If the value is already a UTCDateTime instance, we don't need to parse it.
161+ if ($ value instanceof UTCDateTime)
160162 {
161163 return $ value ;
162164 }
@@ -167,8 +169,7 @@ public function fromDateTime($value)
167169 $ value = parent ::asDateTime ($ value );
168170 }
169171
170-
171- return new MongoDB \BSON \UTCDateTime ($ value ->getTimestamp ());
172+ return new UTCDateTime ($ value ->getTimestamp () * 1000 );
172173 }
173174
174175 /**
@@ -179,10 +180,10 @@ public function fromDateTime($value)
179180 */
180181 protected function asDateTime ($ value )
181182 {
182- // Convert MongoDB\BSON\ UTCDateTime instances.
183- if ($ value instanceof MongoDB \ BSON \ UTCDateTime)
183+ // Convert UTCDateTime instances.
184+ if ($ value instanceof UTCDateTime)
184185 {
185- return Carbon::createFromTimestamp ($ value ->sec );
186+ return Carbon::createFromTimestamp ($ value ->toDateTime ()-> getTimestamp () );
186187 }
187188
188189 return parent ::asDateTime ($ value );
@@ -201,11 +202,11 @@ protected function getDateFormat()
201202 /**
202203 * Get a fresh timestamp for the model.
203204 *
204- * @return MongoDB\BSON\ UTCDateTime
205+ * @return UTCDateTime
205206 */
206207 public function freshTimestamp ()
207- {
208- return round (microtime (true ) * 1000 );
208+ {
209+ return new UTCDateTime ( round (microtime (true ) * 1000 ) );
209210 }
210211
211212 /**
@@ -297,7 +298,7 @@ protected function getAttributeFromArray($key)
297298 */
298299 public function setAttribute ($ key , $ value )
299300 {
300- // Convert _id to MongoDB\BSON\ ObjectID.
301+ // Convert _id to ObjectID.
301302 if ($ key == '_id ' and is_string ($ value ))
302303 {
303304 $ builder = $ this ->newBaseQueryBuilder ();
@@ -336,7 +337,7 @@ public function attributesToArray()
336337 // nicely when your models are converted to JSON.
337338 foreach ($ attributes as $ key => &$ value )
338339 {
339- if ($ value instanceof MongoDB \ BSON \ ObjectID)
340+ if ($ value instanceof ObjectID)
340341 {
341342 $ value = (string ) $ value ;
342343 }
@@ -354,6 +355,39 @@ public function attributesToArray()
354355 return $ attributes ;
355356 }
356357
358+ /**
359+ * Get the casts array.
360+ *
361+ * @return array
362+ */
363+ protected function getCasts ()
364+ {
365+ return $ this ->casts ;
366+ }
367+
368+ /**
369+ * Determine if the new and old values for a given key are numerically equivalent.
370+ *
371+ * @param string $key
372+ * @return bool
373+ */
374+ protected function originalIsNumericallyEquivalent ($ key )
375+ {
376+ $ current = $ this ->attributes [$ key ];
377+ $ original = $ this ->original [$ key ];
378+
379+ // Date comparison.
380+ if (in_array ($ key , $ this ->getDates ()))
381+ {
382+ $ current = $ current instanceof UTCDateTime ? $ this ->asDateTime ($ current ) : $ current ;
383+ $ original = $ original instanceof UTCDateTime ? $ this ->asDateTime ($ original ) : $ original ;
384+
385+ return $ current == $ original ;
386+ }
387+
388+ return parent ::originalIsNumericallyEquivalent ($ key );
389+ }
390+
357391 /**
358392 * Remove one or more fields.
359393 *
0 commit comments