lithium\data\source\MongoDb::create PHP Method

create() public method

Create new document
public create ( string $query, array $options = [] ) : boolean
$query string
$options array
return boolean
    public function create($query, array $options = array())
    {
        $_config = $this->_config;
        $defaults = array('w' => $_config['w'], 'wTimeoutMS' => $_config['wTimeoutMS'], 'fsync' => false);
        $options += $defaults;
        $this->_checkConnection();
        $params = compact('query', 'options');
        $_exp = $this->_classes['exporter'];
        return $this->_filter(__METHOD__, $params, function ($self, $params) use($_config, $_exp) {
            $query = $params['query'];
            $options = $params['options'];
            $args = $query->export($self, array('keys' => array('source', 'data')));
            $data = $_exp::get('create', $args['data']);
            $source = $args['source'];
            if ($source === "{$_config['gridPrefix']}.files" && isset($data['create']['file'])) {
                $result = array('ok' => true);
                $data['create']['_id'] = $self->invokeMethod('_saveFile', array($data['create']));
            } else {
                $result = $self->connection->{$source}->insert($data['create'], $options);
                $result = $self->invokeMethod('_ok', array($result));
            }
            if ($result === true || isset($result['ok']) && (bool) $result['ok'] === true) {
                if ($query->entity()) {
                    $query->entity()->sync($data['create']['_id']);
                }
                return true;
            }
            return false;
        });
    }

Usage Example

Ejemplo n.º 1
0
 public function testCreateNoConnectionException()
 {
     $db = new MongoDb(array('host' => '__invalid__', 'autoConnect' => false));
     $this->assertException('Could not connect to the database.', function () use($db) {
         $db->create(null);
     });
 }
All Usage Examples Of lithium\data\source\MongoDb::create