datatable.directory codebase https://datatable.directory/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
datatable.directory/app/Models/Revision.php

79 lines
2.1 KiB

6 years ago
<?php
namespace App\Models;
use App\Tables\Column;
use Illuminate\Support\Collection;
6 years ago
use Riesjart\Relaquent\Model\Concerns\HasRelaquentRelationships;
/**
* Table revision (a set of rows)
*
* @property int $id
* @property \Carbon\Carbon $created_at
* @property \Carbon\Carbon $updated_at
* @property int $ancestor_id
* @property int $proposal_id
* @property string $note
6 years ago
* @property object $columns
* @property int $row_count - cached number of rows in the revision
* @property-read Revision|null $parentRevision
* @property-read Row[]|Collection $rows
* @property-read Proposal|null $proposal - proposal that was used to create this revision
* @property-read Proposal[]|Collection $dependentProposals
6 years ago
*/
class Revision extends BaseModel
6 years ago
{
use HasRelaquentRelationships;
6 years ago
protected $guarded = [];
6 years ago
protected $casts = [
'columns' => 'object',
];
6 years ago
/** Included rows */
public function rows()
{
return $this->belongsToMany(Row::class, 'revision_row_pivot')->as('_row_pivot');
}
/** Included rows
* @param Column[] $columns
* @return \Illuminate\Database\Query\Builder|static
*/
public function rowsData($columns, $withId=true, $named=true)
{
$selects = $withId ? ["data->>'_id' as _id"] : [];
foreach ($columns as $col) {
$selects[] = "data->>'$col->id' as " . ($named ? $col->name : $col->id);
}
return $this->rows()->select([])->selectRaw(implode(', ', $selects));
6 years ago
}
/** Proposal that lead to this revision */
public function proposal()
6 years ago
{
return $this->belongsTo(Proposal::class, 'proposal_id');
6 years ago
}
/** Proposals that depend on this revision */
public function dependentProposals()
{
return $this->hasMany(Proposal::class, 'revision_id');
6 years ago
}
/** Revision this orignates from */
public function parentRevision()
{
return $this->belongsTo(Revision::class, 'ancestor_id');
}
/** Tables referencing this revision */
public function tables()
{
return $this->belongsToMany(Table::class, 'table_revision_pivot');
}
}