timestamp

27 views
Skip to first unread message

Razi

unread,
May 19, 2011, 1:54:36 PM5/19/11
to YiiMongoDbSuite
Is there any way to get a timestamp value stored? I know the _id has
a built-in timestamp - and this works great for the "created on"
timestamp. I'd like to add an "updated on" timestamp to the
document. What is the best way to accomplish this?

In Mongo documentation, they say:
db.foo.insert( { y : new Timestamp(), x : 3 } )

How would I do this in the YiiMongoDbSuite for a EMongoDocument?

Thanks,
Razi

Attila Nagy

unread,
May 20, 2011, 3:21:20 AM5/20/11
to yiimong...@googlegroups.com
Hi!

What you're asking for is basically a feature for triggers (some
fields gets updated automatically on every update/insert). MongoDB
does not have any support for triggers. What you could do is to
override the beforeSave method in EMongoDocument, and implement your
"triggers" there, in php. Don't forget to call the parent
implementation so that events gets fired. For example:

class MyDoc extends EMongoDocument {

public $updated;

public function beforeSave() {
parent::beforeSave();
$this->updated = time();
}
}

--
Nagy Attila Gabor

Kevin

unread,
May 20, 2011, 3:56:18 AM5/20/11
to yiimong...@googlegroups.com
An alternative would also be using the MongoDate object e.g.

$this->updated = new MongoDate();

Mayur Ahir

unread,
Oct 7, 2011, 2:43:23 AM10/7/11
to yiimong...@googlegroups.com
$this->updated = new MongoDate(time());

Is correct solution.
Reply all
Reply to author
Forward
0 new messages