Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for MorphOne relationships #143

Open
wants to merge 1 commit into
base: L6
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/ColumnSortable/Exceptions/ColumnSortableException.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function __construct($message = '', $code = 0, Exception $previous = null
$message = 'Relation \''.$message.'\' does not exist.';
break;
case 2:
$message = 'Relation \''.$message.'\' is not instance of HasOne or BelongsTo.'; //hasMany
$message = 'Relation \''.$message.'\' is not instance of HasOne, MorphOne, or BelongsTo.'; //hasMany
break;
}

Expand Down
30 changes: 26 additions & 4 deletions src/ColumnSortable/Sortable.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use BadMethodCallException;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\Relations\MorphOne;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\Schema;
Expand Down Expand Up @@ -145,8 +147,13 @@ private function parseParameters(array $parameters)
*/
private function queryJoinBuilder($query, $relation)
{
if ($relation instanceof MorphTo) {
throw new \Exception();
}
$relatedTable = $relation->getRelated()->getTable();
$parentTable = $relation->getParent()->getTable();
$morphClass = $relation->getParent()->getMorphClass();
$morphType = null;

if ($parentTable === $relatedTable) {
$query = $query->from($parentTable.' as parent_'.$parentTable);
Expand All @@ -159,12 +166,16 @@ private function queryJoinBuilder($query, $relation)
$parentPrimaryKey = $relation->getQualifiedParentKeyName();
} elseif ($relation instanceof BelongsTo) {
$relatedPrimaryKey = $relation->getQualifiedOwnerKeyName();
$parentPrimaryKey = $relation->getQualifiedForeignKeyName();
$parentPrimaryKey = $relation->getQualifiedForeignKeyName();
} elseif ($relation instanceof MorphOne) {
$relatedPrimaryKey = $relation->getQualifiedForeignKeyName();
$parentPrimaryKey = $relation->getQualifiedParentKeyName();
$morphType = $relation->getQualifiedMorphType();
} else {
throw new \Exception();
}

return $this->formJoin($query, $parentTable, $relatedTable, $parentPrimaryKey, $relatedPrimaryKey);
return $this->formJoin($query, $parentTable, $relatedTable, $parentPrimaryKey, $relatedPrimaryKey, $morphType, $morphClass);
}


Expand Down Expand Up @@ -211,13 +222,24 @@ private function formatToParameters($array)
* @param $relatedTable
* @param $parentPrimaryKey
* @param $relatedPrimaryKey
* @param $morphType
* @param $morphClass
*
* @return mixed
*/
private function formJoin($query, $parentTable, $relatedTable, $parentPrimaryKey, $relatedPrimaryKey)
private function formJoin($query, $parentTable, $relatedTable, $parentPrimaryKey, $relatedPrimaryKey, $morphType = null, $morphClass = null)
{
$joinType = config('columnsortable.join_type', 'leftJoin');

return $query->select($parentTable.'.*')->{$joinType}($relatedTable, $parentPrimaryKey, '=', $relatedPrimaryKey);
$query
->select($parentTable.'.*')
->{$joinType}($relatedTable, function ($join) use ($parentPrimaryKey, $relatedPrimaryKey, $morphType, $morphClass) {
$join->on($parentPrimaryKey, '=', $relatedPrimaryKey);
if (! is_null($morphType) && ! is_null($morphClass)) {
$join->where($morphType, '=', $morphClass);
}
});

return $query;
}
}
49 changes: 49 additions & 0 deletions tests/ColumnSortableTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ class ColumnSortableTraitTest extends \Orchestra\Testbench\TestCase
*/
private $post;

/**
* @var \Image
*/
private $image;

/**
* @var
*/
Expand All @@ -51,6 +56,7 @@ public function setUp(): void
$this->profile = new Profile();
$this->comment = new Comment();
$this->post = new Post();
$this->image = new Image();

$this->configDefaultDirection = 'asc';
}
Expand Down Expand Up @@ -148,6 +154,16 @@ public function testSortableQueryJoinBuilder()
$expectedQuery = $this->comment->newQuery()->from('comments as parent_comments')->select('parent_comments.*')
->leftJoin('comments', 'parent_comments.parent_id', '=', 'comments.id');
$this->assertEquals($expectedQuery->toSql(), $resultQuery->toSql());

$query = $this->user->newQuery()->with(['image']);
$relation = $query->getRelation('image');
$resultQuery = $this->invokeMethod($this->user, 'queryJoinBuilder', [$query, $relation]);
$expectedQuery = $this->user->newQuery()->select('users.*')->leftJoin('images', function ($join) {
$join
->on('users.id', '=', 'images.imageable_id')
->where('images.imageable_type', '=', Image::class);
});
$this->assertEquals($expectedQuery->toSql(), $resultQuery->toSql());
}


Expand Down Expand Up @@ -350,6 +366,11 @@ public function addressSortable($query, $direction)
{
return $query->join('profiles', 'users.id', '=', 'profiles.user_id')->orderBy('address', $direction)->select('users.*');
}

public function image()
{
return $this->morphOne(Image::class, 'imageable');
}
}

/**
Expand Down Expand Up @@ -417,4 +438,32 @@ class Post extends Model
{

use \Kyslik\ColumnSortable\Sortable;

public function image()
{
return $this->morphOne(Image::class, 'imageable');
}
}

class Image extends Model
{

use \Kyslik\ColumnSortable\Sortable;

public $sortable = [
'id',
'name',
'src',
'altText',
'created_at',
'updated_at'
];

/**
* @return \Illuminate\Database\Eloquent\Relations\MorphTo
*/
public function imageable()
{
return $this->morphTo();
}
}