Extends
lithium\core\StaticObject
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
object Returns 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`.Source
public static function create(array $data = array(), array $options = array()) {
$params = compact('data', 'options');
return static::_filter(__FUNCTION__, $params, function($self, $params) {
$data = $params['data'];
$options = $params['options'];
$defaults = array();
foreach ((array) $self::schema() as $field => $config) {
if (isset($config['default'])) {
$defaults[$field] = $config['default'];
}
}
$data = Set::merge(Set::expand($defaults), $data);
return $self::connection()->item($self, $data, $options);
});
}