@@ -42,11 +42,13 @@ abstract class Model extends \Jenssegers\Eloquent\Model {
42
42
*/
43
43
public function getIdAttribute ($ value )
44
44
{
45
- // If there is an actual id attribute, then return that.
46
- if ($ value ) return $ value ;
45
+ if ($ value ) return (string ) $ value ;
47
46
48
- // Return primary key value if present
49
- if (array_key_exists ($ this ->getKeyName (), $ this ->attributes )) return $ this ->attributes [$ this ->getKeyName ()];
47
+ // Return _id as string
48
+ if (array_key_exists ('_id ' , $ this ->attributes ))
49
+ {
50
+ return (string ) $ this ->attributes ['_id ' ];
51
+ }
50
52
}
51
53
52
54
/**
@@ -194,23 +196,23 @@ public function getTable()
194
196
}
195
197
196
198
/**
197
- * Get an attribute from the model.
199
+ * Set a given attribute on the model.
198
200
*
199
201
* @param string $key
200
- * @return mixed
202
+ * @param mixed $value
203
+ * @return void
201
204
*/
202
- public function getAttribute ($ key )
205
+ public function setAttribute ($ key, $ value )
203
206
{
204
- $ attribute = parent ::getAttribute ($ key );
205
-
206
- // If the attribute is a MongoId object, return it as a string.
207
- // This is makes Eloquent relations a lot easier.
208
- if ($ attribute instanceof MongoId)
207
+ // Convert _id to MongoId.
208
+ if ($ key == '_id ' and is_string ($ value ))
209
209
{
210
- return (string ) $ attribute ;
210
+ $ this ->attributes [$ key ] = new MongoId ($ value );
211
+ }
212
+ else
213
+ {
214
+ parent ::setAttribute ($ key , $ value );
211
215
}
212
-
213
- return $ attribute ;
214
216
}
215
217
216
218
/**
0 commit comments