2

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

kobra
  • 4,853
  • 10
  • 32
  • 33

2 Answers2

4

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
}
Nils
  • 2,041
  • 1
  • 15
  • 20
  • Thanks Nils, I was able to test and successfully implement solution #1 and #3, but I am not sure where can I add the filter. #2 is the best option as I don't need to change the db or add sync to every create, update or delete call. Thanks Gautam – kobra Nov 12 '12 at 23:33
  • Since this is something related to the database I would add the filter in the database bootstrap file. – Nils Nov 13 '12 at 17:04
2

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.

Nate Abele
  • 5,771
  • 32
  • 27
  • Thanks Nate, I will implement Nils suggestion till the feature is added to Li3. My project goes live in December 2012, will this feature be available by then. Thanks Gautam – kobra Nov 12 '12 at 21:28