mirror of
https://github.com/flarum/framework.git
synced 2024-12-02 23:23:52 +08:00
Add API to define custom model relationships
This commit is contained in:
parent
05e1fc88b6
commit
78bd2d513d
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user