mirror of
https://github.com/flarum/framework.git
synced 2024-12-05 00:43:39 +08: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:
parent
fcedd8dff9
commit
3b61ecaa1c
|
@ -1,9 +1,11 @@
|
|||
<?php namespace Flarum\Core\Models;
|
||||
|
||||
use Flarum\Core\Support\MappedMorphTo;
|
||||
use Flarum\Core\Support\MappedMorphToTrait;
|
||||
|
||||
class Notification extends Model
|
||||
{
|
||||
use MappedMorphToTrait;
|
||||
|
||||
/**
|
||||
* The table associated with the model.
|
||||
*
|
||||
|
@ -87,30 +89,7 @@ class Notification extends Model
|
|||
|
||||
public function subject()
|
||||
{
|
||||
$name = 'subject';
|
||||
$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
|
||||
);
|
||||
}
|
||||
return $this->mappedMorphTo(static::$subjects, 'subject', 'type', 'subject_id');
|
||||
}
|
||||
|
||||
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
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user