Skip to content

Instantly share code, notes, and snippets.

@mehlah
Created August 7, 2013 04:41
Show Gist options
  • Save mehlah/6171230 to your computer and use it in GitHub Desktop.
Save mehlah/6171230 to your computer and use it in GitHub Desktop.
class BaseModel extends \lithium\data\Model {
public static function create(array $data = array(), array $options = array()) {
$defaults = array('defaults' => true, 'class' => 'entity');
$options += $defaults;
$model = get_called_class();
if (isset($data['_type']) && $data['_type']) {
list($namespace, $name) = preg_split("/\\\[^\\\]*$/", $model);
$model = $namespace . '\\' . Inflector::humanize(Inflector::pluralize($data['_type']));
if (!class_exists($model)) {
throw new RuntimeException("Class `{$model}` not found.");
}
}
return static::_filter(__FUNCTION__, compact('data', 'options'), function($self, $params) use ($model) {
$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' => $model, 'data' => $data) + $params['options'];
return $self::invokeMethod('_instance', array($class, $options));
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment