mirror of
https://github.com/flarum/framework.git
synced 2025-01-19 16:12:50 +08:00
Use morphTo instead of mappedMorphTo
Turns out we don't need MappedMorphTo after all.
This commit is contained in:
parent
a7903bac3a
commit
0b86f5cebc
|
@ -1,7 +1,6 @@
|
|||
<?php namespace Flarum\Core\Notifications;
|
||||
|
||||
use Flarum\Core\Model;
|
||||
use Flarum\Core\Support\MappedMorphToTrait;
|
||||
|
||||
/**
|
||||
* Models a notification record in the database.
|
||||
|
@ -21,8 +20,6 @@ use Flarum\Core\Support\MappedMorphToTrait;
|
|||
*/
|
||||
class Notification extends Model
|
||||
{
|
||||
use MappedMorphToTrait;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
|
@ -114,7 +111,7 @@ class Notification extends Model
|
|||
*/
|
||||
public function subject()
|
||||
{
|
||||
return $this->mappedMorphTo(static::$subjectModels, 'subject', 'type', 'subject_id');
|
||||
return $this->morphTo('subject', 'subjectModel', 'subject_id');
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -26,8 +26,7 @@ class NotificationRepository
|
|||
->skip($offset)
|
||||
->take($limit);
|
||||
|
||||
return Notification::with('subject')
|
||||
->select('notifications.*', 'p.unread_count')
|
||||
return Notification::select('notifications.*', 'p.unread_count')
|
||||
->mergeBindings($primaries->getQuery())
|
||||
->join(app('flarum.db')->raw('('.$primaries->toSql().') p'), 'notifications.id', '=', 'p.id')
|
||||
->latest('time')
|
||||
|
|
|
@ -1,43 +0,0 @@
|
|||
<?php namespace Flarum\Core\Support;
|
||||
|
||||
use Illuminate\Database\Eloquent\Relations\MorphTo;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class MappedMorphTo extends MorphTo
|
||||
{
|
||||
/**
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $map;
|
||||
|
||||
/**
|
||||
* Create a new morph to relationship instance.
|
||||
*
|
||||
* @param \Illuminate\Database\Eloquent\Builder $query
|
||||
* @param \Illuminate\Database\Eloquent\Model $parent
|
||||
* @param string $foreignKey
|
||||
* @param string $otherKey
|
||||
* @param string $type
|
||||
* @param string $relation
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(Builder $query, Model $parent, $foreignKey, $otherKey, $type, $relation, $map)
|
||||
{
|
||||
$this->map = $map;
|
||||
|
||||
parent::__construct($query, $parent, $foreignKey, $otherKey, $type, $relation);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new model instance by type.
|
||||
*
|
||||
* @param string $type
|
||||
* @return \Illuminate\Database\Eloquent\Model
|
||||
*/
|
||||
public function createModelByType($type)
|
||||
{
|
||||
return new $this->map[$type];
|
||||
}
|
||||
}
|
|
@ -1,48 +0,0 @@
|
|||
<?php namespace Flarum\Core\Support;
|
||||
|
||||
trait MappedMorphToTrait
|
||||
{
|
||||
public function mappedMorphTo($classes, $name = null, $type = null, $id = null)
|
||||
{
|
||||
// If no name is provided, we will use the backtrace to get the function name
|
||||
// since that is most likely the name of the polymorphic interface. We can
|
||||
// use that to get both the class and foreign key that will be utilized.
|
||||
if (is_null($name)) {
|
||||
list(, $caller) = debug_backtrace(false, 2);
|
||||
$name = snake_case($caller['function']);
|
||||
}
|
||||
|
||||
list($type, $id) = $this->getMorphs($name, $type, $id);
|
||||
|
||||
// If the type value is null it is probably safe to assume we're eager loading
|
||||
// the relationship. When that is the case we will pass in a dummy query as
|
||||
// there are multiple types in the morph and we can't use single queries.
|
||||
if (is_null($typeValue = $this->$type)) {
|
||||
return new MappedMorphTo(
|
||||
$this->newQuery(),
|
||||
$this,
|
||||
$id,
|
||||
null,
|
||||
$type,
|
||||
$name,
|
||||
$classes
|
||||
);
|
||||
} else {
|
||||
// If we are not eager loading the relationship we will essentially treat this
|
||||
// as a belongs-to style relationship since morph-to extends that class and
|
||||
// we will pass in the appropriate values so that it behaves as expected.
|
||||
$class = $classes[$typeValue];
|
||||
$instance = new $class;
|
||||
|
||||
return new MappedMorphTo(
|
||||
$instance->newQuery(),
|
||||
$this,
|
||||
$id,
|
||||
$instance->getKeyName(),
|
||||
$type,
|
||||
$name,
|
||||
$classes
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user