diff --git a/lib/Model.php b/lib/Model.php index 7ac45b0d4..444a828d6 100644 --- a/lib/Model.php +++ b/lib/Model.php @@ -1358,6 +1358,12 @@ public static function __callStatic($method, $args) $options = static::extract_and_validate_options($args); $create = false; + $class = get_called_class(); + $method = static::normalize_method($method); + if (method_exists($class, $method)) { + return call_user_func_array(array($class, $method), $args); + } + if (substr($method,0,17) == 'find_or_create_by') { $attributes = substr($method,17); @@ -1403,6 +1409,11 @@ public static function __callStatic($method, $args) */ public function __call($method, $args) { + $method = self::normalize_method($method); + if (method_exists($this, $method)) { + return call_user_func_array(array($this, $method), $args); + } + //check for build|create_association methods if (preg_match('/(build|create)_/', $method)) { @@ -1426,6 +1437,17 @@ public function __call($method, $args) throw new ActiveRecordException("Call to undefined method: $method"); } + /** + * Convert a camelCase method name to a snake_case method name + * + * @param string $method The possibly camelCase method name + * @return string The normalized snake_case method name + */ + protected static function normalize_method($method) + { + return strtolower(preg_replace('/([a-z0-9])([A-Z])/', '$1_$2', $method)); + } + /** * Alias for self::find('all'). *