Created
August 7, 2013 04:41
-
-
Save mehlah/6171230 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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