/** * Define write concern for all requests to current collection * * @param string|integer $w write concern * @param int $timeout timeout in milliseconds * @throws \Sokil\Mongo\Exception * @return \Sokil\Mongo\Collection */ public function setWriteConcern($w, $timeout = 10000) { if (!$this->_mongoCollection->setWriteConcern($w, (int) $timeout)) { throw new Exception('Error setting write concern'); } return $this; }
/** * Constructor * * @param Mongo|MongoClient|array|Traversable $mongo * @param string|MongoDB $database * @param string $collection * @param array $saveOptions * @throws Exception\InvalidArgumentException */ public function __construct($mongo, $database = null, $collection = null, array $saveOptions = array()) { if ($mongo instanceof Traversable) { // Configuration may be multi-dimensional due to save options $mongo = ArrayUtils::iteratorToArray($mongo); } if (is_array($mongo)) { $saveOptions = isset($mongo['save_options']) ? $mongo['save_options'] : array(); $collection = isset($mongo['collection']) ? $mongo['collection'] : null; $database = isset($mongo['database']) ? $mongo['database'] : null; $mongo = isset($mongo['mongo']) ? $mongo['mongo'] : null; } if (null === $collection) { throw new Exception\InvalidArgumentException('The collection parameter cannot be empty'); } if (null === $database) { throw new Exception\InvalidArgumentException('The database parameter cannot be empty'); } if (!($mongo instanceof MongoClient || $mongo instanceof Mongo)) { throw new Exception\InvalidArgumentException(sprintf('Parameter of type %s is invalid; must be MongoClient or Mongo', is_object($mongo) ? get_class($mongo) : gettype($mongo))); } $this->mongoCollection = $mongo->selectCollection($database, $collection); // Set write concern $this->mongoCollection->setWriteConcern(0); $this->saveOptions = $saveOptions; }