mirror of
https://github.com/flarum/framework.git
synced 2025-02-21 09:11:40 +08:00
Allow extensions to add default model attributes
Extensions can add default column values in their migrations, but Eloquent doesn't know about this when it first saves a model to the database. This is useful in flarum-ext-approval where the default value for is_approved on the posts table is true.
This commit is contained in:
parent
40a78d302e
commit
c3dfa3560a
@ -11,6 +11,7 @@
|
||||
namespace Flarum\Database;
|
||||
|
||||
use Flarum\Event\ConfigureModelDates;
|
||||
use Flarum\Event\ConfigureModelDefaultAttributes;
|
||||
use Flarum\Event\GetModelRelationship;
|
||||
use Illuminate\Database\Eloquent\Model as Eloquent;
|
||||
use Illuminate\Database\Eloquent\Relations\Relation;
|
||||
@ -66,6 +67,22 @@ abstract class AbstractModel extends Eloquent
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function __construct(array $attributes = [])
|
||||
{
|
||||
$defaults = [];
|
||||
|
||||
static::$dispatcher->fire(
|
||||
new ConfigureModelDefaultAttributes($this, $defaults)
|
||||
);
|
||||
|
||||
$this->attributes = $defaults;
|
||||
|
||||
parent::__construct($attributes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the attributes that should be converted to dates.
|
||||
*
|
||||
|
45
src/Event/ConfigureModelDefaultAttributes.php
Normal file
45
src/Event/ConfigureModelDefaultAttributes.php
Normal file
@ -0,0 +1,45 @@
|
||||
<?php
|
||||
/*
|
||||
* This file is part of Flarum.
|
||||
*
|
||||
* (c) Toby Zerner <toby.zerner@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Flarum\Event;
|
||||
|
||||
use Flarum\Database\AbstractModel;
|
||||
|
||||
class ConfigureModelDefaultAttributes
|
||||
{
|
||||
/**
|
||||
* @var AbstractModel
|
||||
*/
|
||||
public $model;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
public $attributes;
|
||||
|
||||
/**
|
||||
* @param AbstractModel $model
|
||||
* @param array $attributes
|
||||
*/
|
||||
public function __construct(AbstractModel $model, array &$attributes)
|
||||
{
|
||||
$this->model = $model;
|
||||
$this->attributes = &$attributes;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $model
|
||||
* @return bool
|
||||
*/
|
||||
public function isModel($model)
|
||||
{
|
||||
return $this->model instanceof $model;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user