lithium\data\source\http\adapter\CouchDb::create()

public method

Create new document.

Parameters

  • string $query
  • array $options

Returns

boolean

Filter

This method can be filtered.

Source

	public function create($query, array $options = array()) {
		$defaults = array('model' => $query->model());
		$options += $defaults;
		$params = compact('query', 'options');
		$conn =& $this->connection;
		$config = $this->_config;

		return $this->_filter(__METHOD__, $params, function($self, $params) use (&$conn, $config) {
			$request = array('type' => 'json');
			$query = $params['query'];
			$options = $params['options'];
			$data = $query->data();
			$data += array('type' => $options['model']::meta('source'));

			if (isset($data['id'])) {
				return $self->update($query, $options);
			}

			$retry = false;
			do {
				$result = $conn->post($config['database'], $data, $request);
				$result = is_string($result) ? json_decode($result, true) : $result;
				$retry = $retry ? !$retry : $self->invokeMethod('_autoBuild', array($result));
			} while ($retry);

			if (isset($result['_id']) || (isset($result['ok']) && $result['ok'] === true)) {
				$result = $self->invokeMethod('_format', array($result, $options));
				$query->entity()->sync($result['id'], $result);
				return true;
			}
			return false;
		});
	}