15
15
use Illuminate \Support \Str ;
16
16
use function in_array ;
17
17
use Jenssegers \Mongodb \Query \Builder as QueryBuilder ;
18
- use MongoDB \BSON \Binary ;
19
- use MongoDB \BSON \ObjectID ;
20
18
use MongoDB \BSON \UTCDateTime ;
21
19
use function uniqid ;
22
20
@@ -52,30 +50,6 @@ abstract class Model extends BaseModel
52
50
*/
53
51
protected $ parentRelation ;
54
52
55
- /**
56
- * Custom accessor for the model's id.
57
- *
58
- * @param mixed $value
59
- * @return mixed
60
- */
61
- public function getIdAttribute ($ value = null )
62
- {
63
- // If we don't have a value for 'id', we will use the MongoDB '_id' value.
64
- // This allows us to work with models in a more sql-like way.
65
- if (! $ value && array_key_exists ('_id ' , $ this ->attributes )) {
66
- $ value = $ this ->attributes ['_id ' ];
67
- }
68
-
69
- // Convert ObjectID to string.
70
- if ($ value instanceof ObjectID) {
71
- return (string ) $ value ;
72
- } elseif ($ value instanceof Binary) {
73
- return (string ) $ value ->getData ();
74
- }
75
-
76
- return $ value ;
77
- }
78
-
79
53
/**
80
54
* @inheritdoc
81
55
*/
@@ -190,12 +164,7 @@ protected function getAttributeFromArray($key)
190
164
public function setAttribute ($ key , $ value )
191
165
{
192
166
// Convert _id to ObjectID.
193
- if ($ key == '_id ' && is_string ($ value )) {
194
- $ builder = $ this ->newBaseQueryBuilder ();
195
-
196
- $ value = $ builder ->convertKey ($ value );
197
- } // Support keys in dot notation.
198
- elseif (Str::contains ($ key , '. ' )) {
167
+ if (Str::contains ($ key , '. ' )) {
199
168
// Store to a temporary key, then move data to the actual key
200
169
$ uniqueKey = uniqid ($ key );
201
170
parent ::setAttribute ($ uniqueKey , $ value );
@@ -209,28 +178,6 @@ public function setAttribute($key, $value)
209
178
return parent ::setAttribute ($ key , $ value );
210
179
}
211
180
212
- /**
213
- * @inheritdoc
214
- */
215
- public function attributesToArray ()
216
- {
217
- $ attributes = parent ::attributesToArray ();
218
-
219
- // Because the original Eloquent never returns objects, we convert
220
- // MongoDB related objects to a string representation. This kind
221
- // of mimics the SQL behaviour so that dates are formatted
222
- // nicely when your models are converted to JSON.
223
- foreach ($ attributes as $ key => &$ value ) {
224
- if ($ value instanceof ObjectID) {
225
- $ value = (string ) $ value ;
226
- } elseif ($ value instanceof Binary) {
227
- $ value = (string ) $ value ->getData ();
228
- }
229
- }
230
-
231
- return $ attributes ;
232
- }
233
-
234
181
/**
235
182
* @inheritdoc
236
183
*/
@@ -568,4 +515,23 @@ protected function addCastAttributesToArray(array $attributes, array $mutatedAtt
568
515
569
516
return $ attributes ;
570
517
}
518
+
519
+ /** @internal */
520
+ public function convertKey ($ value )
521
+ {
522
+ if (! $ this ->hasCast ($ this ->primaryKey )) {
523
+ return $ value ;
524
+ }
525
+
526
+ return $ this ->castAttribute ($ this ->primaryKey , $ value );
527
+ }
528
+
529
+ protected function getClassCastableAttributeValue ($ key , $ value )
530
+ {
531
+ // The class cast cache does not play nice with database values that
532
+ // already are objects, so we need to manually unset it
533
+ unset($ this ->classCastCache [$ key ]);
534
+
535
+ return parent ::getClassCastableAttributeValue ($ key , $ value );
536
+ }
571
537
}
0 commit comments