lithium\data\source\Database::create()

public method

Inserts a new record into the database based on a the Query. The record is updated with the id of the insert.

Parameters

  • object $query

    An SQL query string, or lithium\data\model\Query object instance.

  • array $options

    If $query is a string, $options contains an array of bind values to be escaped, quoted, and inserted into $query using Text::insert().

Returns

boolean

Returns true if the query succeeded, otherwise false.

Filter

This method can be filtered.

Source

	public function create($query, array $options = []) {
		$params = compact('query', 'options');

		return Filters::run($this, __FUNCTION__, $params, function($params) {
			$query = $params['query'];
			$model = $entity = $object = $id = null;

			if (is_object($query)) {
				$object = $query;
				$model = $query->model();
				$params = $query->export($this);
				$entity =& $query->entity();
				$query = $this->renderCommand('create', $params, $query);
			} else {
				$query = Text::insert($query, $this->value($params['options']));
			}

			if (!$this->_execute($query)) {
				return false;
			}

			if ($entity) {
				if (($model) && !$model::key($entity)) {
					$id = $this->_insertId($object);
				}
				$entity->sync($id);
			}
			return true;
		});
	}