mirror of
https://github.com/flarum/core.git
synced 2025-07-22 01:01:28 +02:00
Extract mappedMorphTo function into a trait
Not sure if this is the best thing to do, it could also just be put on the base Model class
This commit is contained in:
@@ -1,9 +1,11 @@
|
|||||||
<?php namespace Flarum\Core\Models;
|
<?php namespace Flarum\Core\Models;
|
||||||
|
|
||||||
use Flarum\Core\Support\MappedMorphTo;
|
use Flarum\Core\Support\MappedMorphToTrait;
|
||||||
|
|
||||||
class Notification extends Model
|
class Notification extends Model
|
||||||
{
|
{
|
||||||
|
use MappedMorphToTrait;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The table associated with the model.
|
* The table associated with the model.
|
||||||
*
|
*
|
||||||
@@ -87,30 +89,7 @@ class Notification extends Model
|
|||||||
|
|
||||||
public function subject()
|
public function subject()
|
||||||
{
|
{
|
||||||
$name = 'subject';
|
return $this->mappedMorphTo(static::$subjects, 'subject', 'type', 'subject_id');
|
||||||
$typeColumn = 'type';
|
|
||||||
$idColumn = 'subject_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($type = $this->$typeColumn)) {
|
|
||||||
return new MappedMorphTo(
|
|
||||||
$this->newQuery(), $this, $idColumn, null, $typeColumn, $name, static::$subjects
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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.
|
|
||||||
else {
|
|
||||||
$class = static::$subjects[$type];
|
|
||||||
$instance = new $class;
|
|
||||||
|
|
||||||
return new MappedMorphTo(
|
|
||||||
$instance->newQuery(), $this, $idColumn, $instance->getKeyName(), $typeColumn, $name, static::$subjects
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function getTypes()
|
public static function getTypes()
|
||||||
|
39
framework/core/src/Core/Support/MappedMorphToTrait.php
Normal file
39
framework/core/src/Core/Support/MappedMorphToTrait.php
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
<?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
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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.
|
||||||
|
else {
|
||||||
|
$class = $classes[$typeValue];
|
||||||
|
$instance = new $class;
|
||||||
|
|
||||||
|
return new MappedMorphTo(
|
||||||
|
$instance->newQuery(), $this, $id, $instance->getKeyName(), $type, $name, $classes
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user