belongsTo(User::class, 'author_id'); } /** Target revision */ public function revision() { return $this->belongsTo(Revision::class); } /** Target table (that this was submitted to) */ public function table() { return $this->belongsTo(Table::class); } public function getChangesAttribute($value) { $changeset = Changeset::fromObject(fromJSON($value)); $changeset->revision = $this->revision; $changeset->table = $this->table; $changeset->note = $this->note; return $changeset; } public function setChangesAttribute($value) { if ($value instanceof Changeset) { $this->attributes['changes'] = toJSON($value->toObject()); } else { throw new NotApplicableException("Only a Changeset may be set to Proposal->changes"); } } /** * Create a new Proposal instance wrapping this changeset, * owned by the currently logged in User. The created instance * is NOT saved yet. * * @param Changeset $changeset - changeset to hydrate the proposal with * @return Proposal */ public static function fromChangeset(Changeset $changeset) { if (!$changeset->hasAnyChanges()) { throw new NotApplicableException('No changes to propose.'); } if (empty($changeset->note)) { throw new NotApplicableException('Proposal note must be filled.'); } if ($changeset->table == null || !$changeset->table instanceof Table) { throw new NotApplicableException('Table not assigned to Changeset'); } if ($changeset->revision == null || !$changeset->revision instanceof Revision) { throw new NotApplicableException('Revision not assigned to Changeset'); } // Assign unique row IDs to new rows (they use temporary negative IDs in a draft) $changeset->renumberRows(); return new Proposal([ // relations 'table_id' => $changeset->table->getKey(), 'revision_id' => $changeset->revision->getKey(), 'author_id' => \Auth::user()->getKey(), // the proposal info 'note' => $changeset->note, 'changes' => $changeset->toObject(), ]); } }