Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions lib/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why even have this method_exists() check block?
This won't allow for the build or create methods and this __call method already handles the error case if a method doesn't exist.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same goes for the static version above.


//check for build|create_association methods
if (preg_match('/(build|create)_/', $method))
{
Expand All @@ -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').
*
Expand Down