|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Row in a data table
|
|
|
|
*
|
|
|
|
* @property int $id
|
|
|
|
* @property string $data - JSONB, always containing _grid
|
|
|
|
*/
|
|
|
|
class Row extends BaseModel
|
|
|
|
{
|
|
|
|
protected $casts = [
|
|
|
|
'data' => 'object',
|
|
|
|
];
|
|
|
|
|
|
|
|
protected $guarded = [];
|
|
|
|
public $timestamps = false;
|
|
|
|
|
|
|
|
public function getGridAttribute() {
|
|
|
|
return $this->data->_grid;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setGridAttribute($value) {
|
|
|
|
$this->data->_grid = $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Allocate a single Global Row ID, application-unique.
|
|
|
|
*
|
|
|
|
* GRIDs are used to uniquely identify existing or proposed new rows,
|
|
|
|
* and are preserved after row modifications, to ensure change proposals have
|
|
|
|
* a clear target.
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public static function allocateGRID()
|
|
|
|
{
|
|
|
|
return \DB::selectOne("SELECT nextval('global_row_id_seq') AS grid;")->grid;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Allocate a block of Global Row IDs, application-unique.
|
|
|
|
*
|
|
|
|
* @see Row::allocateGRID()
|
|
|
|
*
|
|
|
|
* @return int[] first and last
|
|
|
|
*/
|
|
|
|
public static function allocateGRIDs($count)
|
|
|
|
{
|
|
|
|
$last = \DB::selectOne("SELECT multi_nextval('global_row_id_seq', ?) AS last_grid;", [$count])->last_grid;
|
|
|
|
return [$last - $count + 1, $last];
|
|
|
|
}
|
|
|
|
}
|