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 String::insert().

Returns

boolean

Returns true if the query succeeded, otherwise false.

Filter

This method can be filtered.

Source

	public function create($query, array $options = array()) {
		return $this->_filter(__METHOD__, compact('query', 'options'), function($self, $params) {
			$query = $params['query'];
			$model = $entity = $object = $id = null;

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

			if (!$self->invokeMethod('_execute', array($query))) {
				return false;
			}

			if ($entity) {
				if (($model) && !$model::key($entity)) {
					$id = $self->invokeMethod('_insertId', array($object));
				}
				$entity->sync($id);
			}
			return true;
		});
	}