Fix problem with columns named 'id', 'data' etc

master
Ondřej Hruška 6 years ago
parent 7969e9bbf4
commit 6c2d19b55c
Signed by: MightyPork
GPG Key ID: 2C5FD5035250423D
  1. 2
      app/Http/Controllers/TableController.php
  2. 2
      app/Http/Controllers/TableEditController.php
  3. 22
      app/Models/Row.php
  4. 4
      app/Tables/BaseExporter.php
  5. 5
      app/Tables/Changeset.php
  6. 4
      resources/views/table/_rows.blade.php

@ -3,6 +3,7 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use App\Models\Revision; use App\Models\Revision;
use App\Models\Row;
use App\Models\Table; use App\Models\Table;
use App\Models\User; use App\Models\User;
use App\Tables\Changeset; use App\Tables\Changeset;
@ -79,6 +80,7 @@ class TableController extends Controller
if (count($columns)) $rq = $rq->sortByCol($columns[0]); if (count($columns)) $rq = $rq->sortByCol($columns[0]);
$rows = $rq->paginate(25, []); $rows = $rq->paginate(25, []);
Row::disableCasts();
return view('table.view', [ return view('table.view', [
'table' => $tableModel, 'table' => $tableModel,
'revision' => $revision, 'revision' => $revision,

@ -4,6 +4,7 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use App\Models\Proposal; use App\Models\Proposal;
use App\Models\Row;
use App\Models\Table; use App\Models\Table;
use App\Models\User; use App\Models\User;
use App\Tables\Changeset; use App\Tables\Changeset;
@ -101,6 +102,7 @@ class TableEditController extends Controller
->sortByCol($changeset->fetchRevisionColumns()[0]) ->sortByCol($changeset->fetchRevisionColumns()[0])
->paginate(25, []); ->paginate(25, []);
Row::disableCasts();
return view('table.propose.edit-rows', [ return view('table.propose.edit-rows', [
'changeset' => $changeset, 'changeset' => $changeset,
'table' => $changeset->table, 'table' => $changeset->table,

@ -13,6 +13,9 @@ use Illuminate\Database\Eloquent\Builder;
*/ */
class Row extends BaseModel class Row extends BaseModel
{ {
protected static $noCasts = false;
protected static $noCastsStack = [];
protected $casts = [ protected $casts = [
'data' => 'object', 'data' => 'object',
]; ];
@ -20,6 +23,25 @@ class Row extends BaseModel
protected $guarded = []; protected $guarded = [];
public $timestamps = false; 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() { public function getRowIdAttribute() {
return $this->data->_id; return $this->data->_id;
} }

@ -4,6 +4,7 @@
namespace App\Tables; namespace App\Tables;
use App\Models\Row;
use App\Models\Table; use App\Models\Table;
/** /**
@ -127,6 +128,7 @@ abstract class BaseExporter
$count = $revision->rows()->count(); $count = $revision->rows()->count();
$start = 0; $start = 0;
$rq = $revision->rowsData($this->columns, false); $rq = $revision->rowsData($this->columns, false);
$columns = $this->columns; $columns = $this->columns;
@ -134,6 +136,7 @@ abstract class BaseExporter
// TODO raw query to allow selecting aggregates, column subsets, etc // TODO raw query to allow selecting aggregates, column subsets, etc
if (count($columns)) $rq = $rq->sortByCol($columns[0]); if (count($columns)) $rq = $rq->sortByCol($columns[0]);
Row::disableCasts();
while ($start < $count) { while ($start < $count) {
$rows = $rq->offset($start)->limit($chunkSize)->get()->toArray(); $rows = $rq->offset($start)->limit($chunkSize)->get()->toArray();
@ -144,6 +147,7 @@ abstract class BaseExporter
$start += $chunkSize; $start += $chunkSize;
} }
Row::enableCasts();
} }
/** /**

@ -435,7 +435,10 @@ class Changeset
unset($r->pivot_revision_id); unset($r->pivot_revision_id);
unset($r->pivot_row_id); unset($r->pivot_row_id);
return (object)$r->getAttributes(); Row::disableCasts();
$vals = (object)$r->getAttributes();
Row::enableCasts();
return $vals;
} }
/** /**

@ -3,7 +3,7 @@
--}} --}}
@php @php
/** @var object[] $columns */ /** @var \App\Tables\Column[] $columns */
/** @var \App\Tables\Changeset[] $changeset */ /** @var \App\Tables\Changeset[] $changeset */
/** @var \App\Models\Row[] $rows */ /** @var \App\Models\Row[] $rows */
@endphp @endphp
@ -11,7 +11,6 @@
<table class="table table-hover table-sm"> <table class="table table-hover table-sm">
<thead> <thead>
<tr> <tr>
{{--<th>#</th>--}}
@foreach($columns as $col) @foreach($columns as $col)
<th title="{{$col->name}}">{{ $col->title }}</th> <th title="{{$col->name}}">{{ $col->title }}</th>
@endforeach @endforeach
@ -20,7 +19,6 @@
<tbody> <tbody>
@foreach($rows as $row) @foreach($rows as $row)
<tr> <tr>
{{--<td>{{$row->_id}}</td>--}}
@foreach($columns as $col) @foreach($columns as $col)
<td>{{ $row->{$col->name} }}</td> <td>{{ $row->{$col->name} }}</td>
@endforeach @endforeach

Loading…
Cancel
Save