Question

I am using PHP Lithium Framework. Is there a way to set safe => 'majority' through configuration.

I am using mongodb replication and want to make sure that the data is written to majority of replicaset members before the driver returns success.

I am using PHP 5.3 and MongoDB 2.2.x

Thanks Gautam

Was it helpful?

Solution

This is not an direct answer to your question but you can set a default behaviour in the database.

 cfg = rs.conf()
 cfg.settings = {}
 cfg.settings.getLastErrorDefaults = {w: "majority", j: true}
 rs.reconfig(cfg)

And in Lithium you should be able to do this with a filter:

 Connections::get('default')->applyFilter(array('create', 'update', 'delete'), function($self, $params, $chain){
     $params['options']['safe'] = true;
      return $chain->next($self, $params, $chain);
});

Or directly in the query:

if (MyModel::update($query, $conditions, array(‘safe’ => true))) {
    // success
}

OTHER TIPS

We have an open pull request for this that will let you add 'safe' => true to your database configuration. Will be merged shortly.

Otherwise, the solution posted by Nils should work great.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top