From 6c2d19b55c8d50e068726433e8bb69f15398fb66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Hru=C5=A1ka?= Date: Wed, 22 Aug 2018 22:07:16 +0200 Subject: [PATCH] Fix problem with columns named 'id', 'data' etc --- app/Http/Controllers/TableController.php | 2 ++ app/Http/Controllers/TableEditController.php | 2 ++ app/Models/Row.php | 22 ++++++++++++++++++++ app/Tables/BaseExporter.php | 4 ++++ app/Tables/Changeset.php | 5 ++++- resources/views/table/_rows.blade.php | 4 +--- 6 files changed, 35 insertions(+), 4 deletions(-) diff --git a/app/Http/Controllers/TableController.php b/app/Http/Controllers/TableController.php index 442bc37..63c3792 100644 --- a/app/Http/Controllers/TableController.php +++ b/app/Http/Controllers/TableController.php @@ -3,6 +3,7 @@ namespace App\Http\Controllers; use App\Models\Revision; +use App\Models\Row; use App\Models\Table; use App\Models\User; use App\Tables\Changeset; @@ -79,6 +80,7 @@ class TableController extends Controller if (count($columns)) $rq = $rq->sortByCol($columns[0]); $rows = $rq->paginate(25, []); + Row::disableCasts(); return view('table.view', [ 'table' => $tableModel, 'revision' => $revision, diff --git a/app/Http/Controllers/TableEditController.php b/app/Http/Controllers/TableEditController.php index 6f56b9d..a268fda 100644 --- a/app/Http/Controllers/TableEditController.php +++ b/app/Http/Controllers/TableEditController.php @@ -4,6 +4,7 @@ namespace App\Http\Controllers; use App\Models\Proposal; +use App\Models\Row; use App\Models\Table; use App\Models\User; use App\Tables\Changeset; @@ -101,6 +102,7 @@ class TableEditController extends Controller ->sortByCol($changeset->fetchRevisionColumns()[0]) ->paginate(25, []); + Row::disableCasts(); return view('table.propose.edit-rows', [ 'changeset' => $changeset, 'table' => $changeset->table, diff --git a/app/Models/Row.php b/app/Models/Row.php index 58a75c4..2915737 100644 --- a/app/Models/Row.php +++ b/app/Models/Row.php @@ -13,6 +13,9 @@ use Illuminate\Database\Eloquent\Builder; */ class Row extends BaseModel { + protected static $noCasts = false; + protected static $noCastsStack = []; + protected $casts = [ 'data' => 'object', ]; @@ -20,6 +23,25 @@ class Row extends BaseModel protected $guarded = []; public $timestamps = false; + public static function disableCasts() + { + array_push(self::$noCastsStack, self::$noCasts); + self::$noCasts = true; + } + + public static function enableCasts() + { + self::$noCasts = array_pop(self::$noCastsStack); + } + + public function getCasts() + { + // This override is needed to prevent casting 'id' to int when it occurs + // as a field in a result of the rowData query + // (this caused display of zeros in string cols) + return self::$noCasts ? [] : $this->casts; + } + public function getRowIdAttribute() { return $this->data->_id; } diff --git a/app/Tables/BaseExporter.php b/app/Tables/BaseExporter.php index 14723b7..a5db729 100644 --- a/app/Tables/BaseExporter.php +++ b/app/Tables/BaseExporter.php @@ -4,6 +4,7 @@ namespace App\Tables; +use App\Models\Row; use App\Models\Table; /** @@ -127,6 +128,7 @@ abstract class BaseExporter $count = $revision->rows()->count(); $start = 0; + $rq = $revision->rowsData($this->columns, false); $columns = $this->columns; @@ -134,6 +136,7 @@ abstract class BaseExporter // TODO raw query to allow selecting aggregates, column subsets, etc if (count($columns)) $rq = $rq->sortByCol($columns[0]); + Row::disableCasts(); while ($start < $count) { $rows = $rq->offset($start)->limit($chunkSize)->get()->toArray(); @@ -144,6 +147,7 @@ abstract class BaseExporter $start += $chunkSize; } + Row::enableCasts(); } /** diff --git a/app/Tables/Changeset.php b/app/Tables/Changeset.php index 864d1ef..af0119f 100644 --- a/app/Tables/Changeset.php +++ b/app/Tables/Changeset.php @@ -435,7 +435,10 @@ class Changeset unset($r->pivot_revision_id); unset($r->pivot_row_id); - return (object)$r->getAttributes(); + Row::disableCasts(); + $vals = (object)$r->getAttributes(); + Row::enableCasts(); + return $vals; } /** diff --git a/resources/views/table/_rows.blade.php b/resources/views/table/_rows.blade.php index effce66..67466b0 100644 --- a/resources/views/table/_rows.blade.php +++ b/resources/views/table/_rows.blade.php @@ -3,7 +3,7 @@ --}} @php - /** @var object[] $columns */ + /** @var \App\Tables\Column[] $columns */ /** @var \App\Tables\Changeset[] $changeset */ /** @var \App\Models\Row[] $rows */ @endphp @@ -11,7 +11,6 @@ - {{----}} @foreach($columns as $col) @endforeach @@ -20,7 +19,6 @@ @foreach($rows as $row) - {{----}} @foreach($columns as $col) @endforeach
#{{ $col->title }}
{{$row->_id}}{{ $row->{$col->name} }}