diff --git a/src/Core/Models/Discussion.php b/src/Core/Models/Discussion.php index bb409cf98..7444084bc 100755 --- a/src/Core/Models/Discussion.php +++ b/src/Core/Models/Discussion.php @@ -47,7 +47,6 @@ class Discussion extends Model * * @var array */ - protected $dates = ['start_time', 'last_time']; /** * An array of posts that have been added during this request. @@ -62,6 +61,7 @@ class Discussion extends Model * @var \Flarum\Core\Models\Post[] */ public $removedPosts = []; + protected static $dates = ['start_time', 'last_time']; /** * The user for which the state relationship should be loaded. diff --git a/src/Core/Models/Model.php b/src/Core/Models/Model.php index e03e5b0d9..e3964d162 100755 --- a/src/Core/Models/Model.php +++ b/src/Core/Models/Model.php @@ -17,19 +17,26 @@ use LogicException; * * @todo Refactor out validation, either into a trait or into a dependency. * The following requirements need to be fulfilled: - * - Ability for extensions to alter ruleset. + * - Ability for extensions to alter ruleset (add, modify, remove). * - Ability for extensions to add custom rules to the validator instance. * - Use Flarum's translator with the validator instance. */ abstract class Model extends Eloquent { /** - * Indicates if the model should be timestamped. Turn them off by default. + * Indicates if the model should be timestamped. Turn off by default. * * @var boolean */ public $timestamps = false; + /** + * The attributes that should be mutated to dates. + * + * @var array + */ + public static $dates = []; + /** * The validation rules for this model. * @@ -162,6 +169,26 @@ abstract class Model extends Eloquent return $rules; } + /** + * Get the attributes that should be converted to dates. + * + * @return array + */ + public function getDates() + { + return static::$dates; + } + + /** + * Add an attribute to be converted to a date. + * + * @param string $attribute + */ + public static function addDate($attribute) + { + static::$dates[] = $attribute; + } + /** * Get an attribute from the model. If nothing is found, attempt to load * a custom relation method with this key. diff --git a/src/Extend/Model.php b/src/Extend/Model.php index 7cf2fa6af..4b8695a34 100644 --- a/src/Extend/Model.php +++ b/src/Extend/Model.php @@ -32,6 +32,11 @@ class Model implements ExtenderInterface return $this; } + public function date($attribute) + { + $this->dates[] = $attribute; + } + public function hasOne($relation, $related, $foreignKey = null, $localKey = null) { $this->relations[$relation] = function ($model) use ($relation, $related, $foreignKey, $localKey) { @@ -73,7 +78,7 @@ class Model implements ExtenderInterface $model = $this->model; foreach ($this->relations as $relation => $callback) { - $model::addRelationship($relation, $callback); + $model::setRelationMethod($relation, $callback); } foreach ($this->scopeVisible as $callback) { @@ -83,5 +88,9 @@ class Model implements ExtenderInterface foreach ($this->allow as $info) { $model::allow($info['action'], $info['callback']); } + + foreach ($this->dates as $attribute) { + $model::addDate($attribute); + } } }