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 = []) {
		$defaults = ['model' => $query->model()];
		$options += $defaults;
		$params = compact('query', 'options');

		return Filters::run($this, __FUNCTION__, $params, function($params) {
			$request = ['type' => 'json'];
			$query = $params['query'];
			$options = $params['options'];
			$data = $query->data();
			$data += ['type' => $options['model']::meta('source')];

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

			$retry = false;
			do {
				$result = $this->connection->post($this->_config['database'], $data, $request);
				$result = is_string($result) ? json_decode($result, true) : $result;

				$retry = $retry ? !$retry : $this->_autoBuild($result);
			} while ($retry);

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