Add API to define custom model relationships

This commit is contained in:
Toby Zerner 2015-05-04 08:55:03 +09:30
parent 05e1fc88b6
commit 78bd2d513d

View File

@ -24,6 +24,13 @@ class Model extends Eloquent
*/
protected static $rules = [];
/**
* The custom relations on this model, registered by extensions.
*
* @var array
*/
protected static $relationships = [];
/**
* The forum model instance.
*
@ -185,4 +192,34 @@ class Model extends Eloquent
throw new PermissionDeniedException;
}
}
/**
* Add a custom relationship to the model.
*
* @param string $name The name of the relationship.
* @param Closure $callback The callback to execute. This should return an
* Eloquent relationship object.
* @return void
*/
public static function addRelationship($name, $callback)
{
static::$relationships[$name] = $callback;
}
/**
* Check for and execute custom relationships.
*
* @param string $name
* @param array $arguments
* @return mixed
*/
public function __call($name, $arguments)
{
if (isset(static::$relationships[$name])) {
array_unshift($arguments, $this);
return call_user_func_array(static::$relationships[$name], $arguments);
}
return parent::__call($name, $arguments);
}
}