lithium\data\source\Database::create()
Implements
lithium\data\Source::create()
Inserts a new record into the database based on a the Query. The record is updated
with the id of the insert.
Parameters
-
object
$queryAn SQL query string, or
lithium\data\model\Queryobject instance. -
array
$optionsIf $query is a string, $options contains an array of bind values to be escaped, quoted, and inserted into
$queryusingText::insert().
Returns
booleanReturns 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;
});
}