Skip to content

Commit f5ffbfb

Browse files
committed
Hide MongoId and MongoDate objects from the user
1 parent 3eaf3b1 commit f5ffbfb

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

src/Jenssegers/Mongodb/Model.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Jenssegers\Mongodb\Builder as QueryBuilder;
66

77
use DateTime;
8+
use MongoId;
89
use MongoDate;
910

1011
abstract class Model extends \Illuminate\Database\Eloquent\Model {
@@ -109,4 +110,31 @@ protected function newBaseQueryBuilder()
109110
return new QueryBuilder($this->getConnection());
110111
}
111112

113+
/**
114+
* Set the array of model attributes. No checking is done.
115+
*
116+
* @param array $attributes
117+
* @param bool $sync
118+
* @return void
119+
*/
120+
public function setRawAttributes(array $attributes, $sync = false)
121+
{
122+
foreach($attributes as $key => &$value)
123+
{
124+
// Convert MongoId
125+
if ($value instanceof MongoId)
126+
{
127+
$value = (string) $value;
128+
}
129+
130+
// Convert MongoDate
131+
else if ($value instanceof MongoDate)
132+
{
133+
$value = $this->asDateTime($value)->format('Y-m-d H:i:s');
134+
}
135+
}
136+
137+
parent::setRawAttributes($attributes, $sync);
138+
}
139+
112140
}

0 commit comments

Comments
 (0)