mirror of
https://github.com/flarum/framework.git
synced 2025-01-19 18:12:59 +08:00
Only get posts with registered types.
This is so that if an extension adds a post type, and the database gets populated with posts of that type, but then if the extension is disabled, we wouldn’t want those posts to display because we would have no knowledge about how to deal with/render them.
This commit is contained in:
parent
38ebb15334
commit
afa4b98c4a
|
@ -68,7 +68,7 @@ class Discussion extends Model
|
|||
static::deleted(function ($discussion) {
|
||||
$discussion->raise(new DiscussionWasDeleted($discussion));
|
||||
|
||||
$discussion->posts()->delete();
|
||||
$discussion->posts()->allTypes()->delete();
|
||||
$discussion->readers()->detach();
|
||||
});
|
||||
}
|
||||
|
|
|
@ -65,6 +65,8 @@ class Post extends Model
|
|||
static::deleted(function ($post) {
|
||||
$post->raise(new PostWasDeleted($post));
|
||||
});
|
||||
|
||||
static::addGlobalScope(new RegisteredTypesScope);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -119,6 +121,18 @@ class Post extends Model
|
|||
return array_map('intval', $query->get(['id'])->fetch('id')->all());
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all posts, regardless of their type, by removing the
|
||||
* `RegisteredTypesScope` global scope constraints applied on this model.
|
||||
*
|
||||
* @param \Illuminate\Database\Eloquent\Builder $query
|
||||
* @return \Illuminate\Database\Eloquent\Builder
|
||||
*/
|
||||
public function scopeAllTypes($query)
|
||||
{
|
||||
return $this->removeGlobalScopes($query);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new model instance according to the post's type.
|
||||
*
|
||||
|
@ -155,4 +169,9 @@ class Post extends Model
|
|||
{
|
||||
static::$types[$type] = $class;
|
||||
}
|
||||
|
||||
public static function getTypes()
|
||||
{
|
||||
return static::$types;
|
||||
}
|
||||
}
|
||||
|
|
55
src/Core/Models/RegisteredTypesScope.php
Normal file
55
src/Core/Models/RegisteredTypesScope.php
Normal file
|
@ -0,0 +1,55 @@
|
|||
<?php namespace Flarum\Core\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\ScopeInterface;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class RegisteredTypesScope implements ScopeInterface
|
||||
{
|
||||
/**
|
||||
* Apply the scope to a given Eloquent query builder.
|
||||
*
|
||||
* @param \Illuminate\Database\Eloquent\Builder $builder
|
||||
* @param \Illuminate\Database\Eloquent\Model $model
|
||||
* @return void
|
||||
*/
|
||||
public function apply(Builder $builder, Model $model)
|
||||
{
|
||||
$builder->whereIn('type', array_keys($model::getTypes()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the scope from the given Eloquent query builder.
|
||||
*
|
||||
* @param \Illuminate\Database\Eloquent\Builder $builder
|
||||
* @param \Illuminate\Database\Eloquent\Model $model
|
||||
* @return void
|
||||
*/
|
||||
public function remove(Builder $builder, Model $model)
|
||||
{
|
||||
$query = $builder->getQuery();
|
||||
|
||||
foreach ((array) $query->wheres as $key => $where)
|
||||
{
|
||||
if ($this->isTypeConstraint($where))
|
||||
{
|
||||
unset($query->wheres[$key]);
|
||||
|
||||
$query->wheres = array_values($query->wheres);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the given where clause is a type constraint.
|
||||
*
|
||||
* @param array $where
|
||||
* @param string $column
|
||||
* @return bool
|
||||
*/
|
||||
protected function isTypeConstraint(array $where)
|
||||
{
|
||||
return $where['type'] == 'In' && $where['column'] == 'type';
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user