MongoCollection::insert PHP Method

insert() public method

Inserts an array into the collection
public insert ( array | object &$a, array $options = [] ) : boolean | array
$a array | object
$options array
return boolean | array Returns an array containing the status of the insertion if the "w" option is set.
    public function insert(&$a, array $options = [])
    {
        if (!$this->ensureDocumentHasMongoId($a)) {
            trigger_error(sprintf('%s(): expects parameter %d to be an array or object, %s given', __METHOD__, 1, gettype($a)), E_USER_WARNING);
            return;
        }
        if (!count((array) $a)) {
            throw new \MongoException('document must be an array or object');
        }
        try {
            $result = $this->collection->insertOne(TypeConverter::fromLegacy($a), $this->convertWriteConcernOptions($options));
        } catch (\MongoDB\Driver\Exception\Exception $e) {
            throw ExceptionConverter::toLegacy($e);
        }
        if (!$result->isAcknowledged()) {
            return true;
        }
        return ['ok' => 1.0, 'n' => 0, 'err' => null, 'errmsg' => null];
    }

Usage Example

 /**
  * Logs a message.
  *
  * @param string $message   Message
  * @param string $priority  Message priority
  */
 protected function doLog($message, $priority)
 {
     $document = array('message' => $message, 'priority' => $priority);
     $event = new sfEvent($this, 'mongodblog.pre_insert');
     $this->dispatcher->filter($event, $document);
     $this->collection->insert($event->getReturnValue(), $this->options['save']);
 }
All Usage Examples Of MongoCollection::insert