From 2156787cc12995ed0d94ef40c6b3c2920fef1b48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Drahos=20Istva=CC=81n?= Date: Thu, 15 Feb 2018 09:42:36 +0100 Subject: [PATCH 1/4] adding softdelete support to hasOne and belongsTo relations --- src/EloquentDataTable.php | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/EloquentDataTable.php b/src/EloquentDataTable.php index 700ac5bc..b52fee05 100644 --- a/src/EloquentDataTable.php +++ b/src/EloquentDataTable.php @@ -138,6 +138,7 @@ protected function isNotEagerLoaded($relation) protected function joinEagerLoadedColumn($relation, $relationColumn) { $table = ''; + $deletedAt = false; $lastQuery = $this->query; foreach (explode('.', $relation) as $eachRelation) { $model = $lastQuery->getRelation($eachRelation); @@ -163,33 +164,44 @@ protected function joinEagerLoadedColumn($relation, $relationColumn) $table = $model->getRelated()->getTable(); $foreign = $model->getQualifiedForeignKeyName(); $other = $model->getQualifiedParentKeyName(); + $deletedAt = $this->checkSoftDeletesOnModel($model->getRelated()); break; case $model instanceof BelongsTo: $table = $model->getRelated()->getTable(); $foreign = $model->getQualifiedForeignKey(); $other = $model->getQualifiedOwnerKeyName(); + $deletedAt = $this->checkSoftDeletesOnModel($model->getRelated()); break; default: throw new Exception('Relation ' . get_class($model) . ' is not yet supported.'); } - $this->performJoin($table, $foreign, $other); + $this->performJoin($table, $foreign, $other, $deletedAt); $lastQuery = $model->getQuery(); } return $table . '.' . $relationColumn; } + protected function checkSoftDeletesOnModel($model){ + if (in_array('Illuminate\Database\Eloquent\SoftDeletes', class_uses($model))) { + return $model->getDeletedAtColumn(); + } + + return false; + } + /** * Perform join query. * * @param string $table * @param string $foreign * @param string $other + * @param string $deletedAt * @param string $type */ - protected function performJoin($table, $foreign, $other, $type = 'left') + protected function performJoin($table, $foreign, $other, $deletedAt = null, $type = 'left') { $joins = []; foreach ((array) $this->getBaseQueryBuilder()->joins as $key => $join) { @@ -199,5 +211,9 @@ protected function performJoin($table, $foreign, $other, $type = 'left') if (! in_array($table, $joins)) { $this->getBaseQueryBuilder()->join($table, $foreign, '=', $other, $type); } + + if ($deletedAt !== false) { + $this->getBaseQueryBuilder()->whereNull($deletedAt); + } } } From d7c106a1c7eda3d1af19e7e1325244c10592f3a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Drahos=20Istva=CC=81n?= Date: Thu, 15 Feb 2018 09:46:20 +0100 Subject: [PATCH 2/4] Revert "adding softdelete support to hasOne and belongsTo relations" This reverts commit 2156787cc12995ed0d94ef40c6b3c2920fef1b48. --- src/EloquentDataTable.php | 20 ++------------------ 1 file changed, 2 insertions(+), 18 deletions(-) diff --git a/src/EloquentDataTable.php b/src/EloquentDataTable.php index b52fee05..700ac5bc 100644 --- a/src/EloquentDataTable.php +++ b/src/EloquentDataTable.php @@ -138,7 +138,6 @@ protected function isNotEagerLoaded($relation) protected function joinEagerLoadedColumn($relation, $relationColumn) { $table = ''; - $deletedAt = false; $lastQuery = $this->query; foreach (explode('.', $relation) as $eachRelation) { $model = $lastQuery->getRelation($eachRelation); @@ -164,44 +163,33 @@ protected function joinEagerLoadedColumn($relation, $relationColumn) $table = $model->getRelated()->getTable(); $foreign = $model->getQualifiedForeignKeyName(); $other = $model->getQualifiedParentKeyName(); - $deletedAt = $this->checkSoftDeletesOnModel($model->getRelated()); break; case $model instanceof BelongsTo: $table = $model->getRelated()->getTable(); $foreign = $model->getQualifiedForeignKey(); $other = $model->getQualifiedOwnerKeyName(); - $deletedAt = $this->checkSoftDeletesOnModel($model->getRelated()); break; default: throw new Exception('Relation ' . get_class($model) . ' is not yet supported.'); } - $this->performJoin($table, $foreign, $other, $deletedAt); + $this->performJoin($table, $foreign, $other); $lastQuery = $model->getQuery(); } return $table . '.' . $relationColumn; } - protected function checkSoftDeletesOnModel($model){ - if (in_array('Illuminate\Database\Eloquent\SoftDeletes', class_uses($model))) { - return $model->getDeletedAtColumn(); - } - - return false; - } - /** * Perform join query. * * @param string $table * @param string $foreign * @param string $other - * @param string $deletedAt * @param string $type */ - protected function performJoin($table, $foreign, $other, $deletedAt = null, $type = 'left') + protected function performJoin($table, $foreign, $other, $type = 'left') { $joins = []; foreach ((array) $this->getBaseQueryBuilder()->joins as $key => $join) { @@ -211,9 +199,5 @@ protected function performJoin($table, $foreign, $other, $deletedAt = null, $typ if (! in_array($table, $joins)) { $this->getBaseQueryBuilder()->join($table, $foreign, '=', $other, $type); } - - if ($deletedAt !== false) { - $this->getBaseQueryBuilder()->whereNull($deletedAt); - } } } From e2679a96ed5f7d8f989ec230581d664640cb6801 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Drahos=20Istva=CC=81n?= Date: Thu, 15 Feb 2018 09:55:30 +0100 Subject: [PATCH 3/4] Support SoftDeletes on join --- src/EloquentDataTable.php | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/EloquentDataTable.php b/src/EloquentDataTable.php index 700ac5bc..216d2b18 100644 --- a/src/EloquentDataTable.php +++ b/src/EloquentDataTable.php @@ -138,6 +138,7 @@ protected function isNotEagerLoaded($relation) protected function joinEagerLoadedColumn($relation, $relationColumn) { $table = ''; + $deletedAt = false; $lastQuery = $this->query; foreach (explode('.', $relation) as $eachRelation) { $model = $lastQuery->getRelation($eachRelation); @@ -163,33 +164,44 @@ protected function joinEagerLoadedColumn($relation, $relationColumn) $table = $model->getRelated()->getTable(); $foreign = $model->getQualifiedForeignKeyName(); $other = $model->getQualifiedParentKeyName(); + $deletedAt = $this->checkSoftDeletesOnModel($model->getRelated()); break; case $model instanceof BelongsTo: $table = $model->getRelated()->getTable(); $foreign = $model->getQualifiedForeignKey(); $other = $model->getQualifiedOwnerKeyName(); + $deletedAt = $this->checkSoftDeletesOnModel($model->getRelated()); break; default: throw new Exception('Relation ' . get_class($model) . ' is not yet supported.'); } - $this->performJoin($table, $foreign, $other); + $this->performJoin($table, $foreign, $other, $deletedAt); $lastQuery = $model->getQuery(); } return $table . '.' . $relationColumn; } + protected function checkSoftDeletesOnModel($model){ + if (in_array('Illuminate\Database\Eloquent\SoftDeletes', class_uses($model))) { + return $model->getDeletedAtColumn(); + } + + return false; + } + /** * Perform join query. * * @param string $table * @param string $foreign * @param string $other + * @param string $deletedAt * @param string $type */ - protected function performJoin($table, $foreign, $other, $type = 'left') + protected function performJoin($table, $foreign, $other, $deletedAt = false, $type = 'left') { $joins = []; foreach ((array) $this->getBaseQueryBuilder()->joins as $key => $join) { @@ -199,5 +211,9 @@ protected function performJoin($table, $foreign, $other, $type = 'left') if (! in_array($table, $joins)) { $this->getBaseQueryBuilder()->join($table, $foreign, '=', $other, $type); } + + if ($deletedAt !== false) { + $this->getBaseQueryBuilder()->whereNull($deletedAt); + } } } From a6bf7bf1d3481406d2ad9502ebf9541369ac88f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Drahos=20Istva=CC=81n?= Date: Thu, 15 Feb 2018 10:35:03 +0100 Subject: [PATCH 4/4] code style issues fix --- src/EloquentDataTable.php | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/EloquentDataTable.php b/src/EloquentDataTable.php index 216d2b18..8c3f9ce8 100644 --- a/src/EloquentDataTable.php +++ b/src/EloquentDataTable.php @@ -161,16 +161,16 @@ protected function joinEagerLoadedColumn($relation, $relationColumn) break; case $model instanceof HasOneOrMany: - $table = $model->getRelated()->getTable(); - $foreign = $model->getQualifiedForeignKeyName(); - $other = $model->getQualifiedParentKeyName(); + $table = $model->getRelated()->getTable(); + $foreign = $model->getQualifiedForeignKeyName(); + $other = $model->getQualifiedParentKeyName(); $deletedAt = $this->checkSoftDeletesOnModel($model->getRelated()); break; case $model instanceof BelongsTo: - $table = $model->getRelated()->getTable(); - $foreign = $model->getQualifiedForeignKey(); - $other = $model->getQualifiedOwnerKeyName(); + $table = $model->getRelated()->getTable(); + $foreign = $model->getQualifiedForeignKey(); + $other = $model->getQualifiedOwnerKeyName(); $deletedAt = $this->checkSoftDeletesOnModel($model->getRelated()); break; @@ -184,7 +184,8 @@ protected function joinEagerLoadedColumn($relation, $relationColumn) return $table . '.' . $relationColumn; } - protected function checkSoftDeletesOnModel($model){ + protected function checkSoftDeletesOnModel($model) + { if (in_array('Illuminate\Database\Eloquent\SoftDeletes', class_uses($model))) { return $model->getDeletedAtColumn(); }