Skip to content

Commit

Permalink
[HttpKernel] Support MongoClient and Mongo connection classes
Browse files Browse the repository at this point in the history
MongoClient defaults its write concern to w=1 (i.e. "safe" writes), which means update() may return an array instead of boolean true. Check for this before returning from write().
  • Loading branch information
jmikola committed Dec 13, 2012
1 parent b28af77 commit de19a81
Showing 1 changed file with 8 additions and 3 deletions.
Expand Up @@ -102,7 +102,9 @@ public function write(Profile $profile)
'time' => $profile->getTime()
);

return $this->getMongo()->update(array('_id' => $profile->getToken()), array_filter($record, function ($v) { return !empty($v); }), array('upsert' => true));
$result = $this->getMongo()->update(array('_id' => $profile->getToken()), array_filter($record, function ($v) { return !empty($v); }), array('upsert' => true));

return isset($result['ok']) ? (boolean) $result['ok'] : true;
}

/**
Expand All @@ -114,12 +116,15 @@ protected function getMongo()
{
if ($this->mongo === null) {
if (preg_match('#^(mongodb://.*)/(.*)/(.*)$#', $this->dsn, $matches)) {
$mongo = new \Mongo($matches[1] . (!empty($matches[2]) ? '/' . $matches[2] : ''));
$server = $matches[1] . (!empty($matches[2]) ? '/' . $matches[2] : '');
$database = $matches[2];
$collection = $matches[3];

$mongoClass = (version_compare(phpversion('mongo'), '1.3.0', '<')) ? '\Mongo' : '\MongoClient';
$mongo = new $mongoClass($server);
$this->mongo = $mongo->selectCollection($database, $collection);
} else {
throw new \RuntimeException(sprintf('Please check your configuration. You are trying to use MongoDB with an invalid dsn "%s". The expected format is "mongodb://user:pass@location/database/collection"', $this->dsn));
throw new \RuntimeException(sprintf('Please check your configuration. You are trying to use MongoDB with an invalid dsn "%s". The expected format is "mongodb://[user:pass@]host/database/collection"', $this->dsn));
}
}

Expand Down

0 comments on commit de19a81

Please sign in to comment.