lithium\data\Model::create()
Instantiates a new record or document object, initialized with any data passed in. For example:
$post = Posts::create(['title' => 'New post']);
echo $post->title; // echoes 'New post'
$success = $post->save();
Note that while this method creates a new object, there is no effect on the database until
the save() method is called.
In addition, this method can be used to simulate loading a pre-existing object from the database, without actually querying the database:
$post = Posts::create(['id' => $id, 'moreData' => 'foo'], ['exists' => true]);
$post->title = 'New title';
$success = $post->save();
This will create an update query against the object with an ID matching $id. Also note that
only the title field will be updated.
Parameters
-
array
$dataAny data that this object should be populated with initially.
-
array
$optionsOptions to be passed to item.
Returns
objectReturns a new, un-saved record or document object. In addition to the values
passed to $data, the object will also contain any values assigned to the
'default' key of each field defined in $_schema.
Filter
This method can be filtered.
Source
public static function create(array $data = [], array $options = []) {
$defaults = ['defaults' => true, 'class' => 'entity'];
$options += $defaults;
$params = compact('data', 'options');
return Filters::run(get_called_class(), __FUNCTION__, $params, function($params) {
$class = $params['options']['class'];
unset($params['options']['class']);
if ($class === 'entity' && $params['options']['defaults']) {
$data = Set::merge(Set::expand(static::schema()->defaults()), $params['data']);
} else {
$data = $params['data'];
}
return Libraries::instance(
null,
$class,
['model' => get_called_class(), 'data' => $data] + $params['options'],
static::object()->_classes
);
});
}