<?php
/**
 * Created by PhpStorm.
 * User: cbarranco
 * Date: 11/3/17
 * Time: 12:59 PM
 */

namespace Hilco\Models;
use DB;

trait HasCommitSequence {
    public function save(array $options = []) {
        //todo so this saves twice no, that's no bueno
        DB::transaction(function () use ($options) {
            $commitSequence = DB::table($this->getTable())->lockForUpdate()->max('commit_sequence') + 1;
            $this->commit_sequence = $commitSequence;
            return parent::save($options);
        });
    }

    public function delete() {
        //todo so this saves twice no, that's no bueno
        DB::transaction(function() {
            $commitSequence = DB::table($this->getTable())->lockForUpdate()->max('commit_sequence') + 1;
            $this->commit_sequence = $commitSequence;
            parent::save();
            parent::delete();
        });
    }
}