lithium\data\Model::create()
Instantiates a new record or document object, initialized with any data passed in. For example:
$post = Posts::create(array('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(array('id' => $id, 'moreData' => 'foo'), array('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
$data
Any data that this object should be populated with initially.
-
array
$options
Options 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(), array $options = array()) {
$defaults = array('defaults' => true, 'class' => 'entity');
$options += $defaults;
return static::_filter(__FUNCTION__, compact('data', 'options'), function($self, $params) {
$class = $params['options']['class'];
unset($params['options']['class']);
if ($class === 'entity' && $params['options']['defaults']) {
$data = Set::merge(Set::expand($self::schema()->defaults()), $params['data']);
} else {
$data = $params['data'];
}
$options = array('model' => $self, 'data' => $data) + $params['options'];
return $self::invokeMethod('_instance', array($class, $options));
});
}