1515use Illuminate \Support \Str ;
1616use function in_array ;
1717use Jenssegers \Mongodb \Query \Builder as QueryBuilder ;
18- use MongoDB \BSON \Binary ;
19- use MongoDB \BSON \ObjectID ;
2018use MongoDB \BSON \UTCDateTime ;
2119use function uniqid ;
2220
@@ -52,30 +50,6 @@ abstract class Model extends BaseModel
5250 */
5351 protected $ parentRelation ;
5452
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-
7953 /**
8054 * @inheritdoc
8155 */
@@ -190,12 +164,7 @@ protected function getAttributeFromArray($key)
190164 public function setAttribute ($ key , $ value )
191165 {
192166 // 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 , '. ' )) {
199168 // Store to a temporary key, then move data to the actual key
200169 $ uniqueKey = uniqid ($ key );
201170 parent ::setAttribute ($ uniqueKey , $ value );
@@ -209,28 +178,6 @@ public function setAttribute($key, $value)
209178 return parent ::setAttribute ($ key , $ value );
210179 }
211180
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-
234181 /**
235182 * @inheritdoc
236183 */
@@ -568,4 +515,23 @@ protected function addCastAttributesToArray(array $attributes, array $mutatedAtt
568515
569516 return $ attributes ;
570517 }
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+ }
571537}
0 commit comments