src/Vzool/Revisionable/RevisionableTrait.php
/**
     * Called after a model is successfully saved.
     *
     * @return void
     */
Called after a model is successfully saved.
@return void
Called after a model is successfully saved . @return void
public function postSave()
    {
        if (isset($this->historyLimit) && $this->revisionHistory()->count() >= $this->historyLimit) {
            $LimitReached = true;
        } else {
            $LimitReached = false;
        }
        if (isset($this->revisionCleanup)){
            $RevisionCleanup=$this->revisionCleanup;
        }else{
            $RevisionCleanup=false;
        }
        if (((!isset($this->revisionEnabled) || $this->revisionEnabled) && $this->updating) && (!$LimitReached || $RevisionCleanup)) {
            $changes_to_record = $this->changedRevisionableFields();
            $revisions = array();
            foreach ($changes_to_record as $key => $change) {
                $original = array(
                    'revisionable_type' => $this->getMorphClass(),
                    'revisionable_id' => $this->getKey(),
                    'key' => $key,
                    'old_value' => Arr::get($this->originalData, $key),
                    'new_value' => $this->updatedData[$key],
                    'user_id' => $this->getSystemUserId(),
                    'ip' => $_SERVER["REMOTE_ADDR"],
                    'created_at' => new \DateTime(),
                    'updated_at' => new \DateTime(),
                );
                $revisions[] = array_merge($original, $this->getAdditionalFields());
            }
            if (count($revisions) > 0) {
                if($LimitReached && $RevisionCleanup){
                    $toDelete = $this->revisionHistory()->orderBy('id','asc')->limit(count($revisions))->get();
                    foreach($toDelete as $delete){
                        $delete->delete();
                    }
                }
                $revision = Revisionable::newModel();
                \DB::table($revision->getTable())->insert($revisions);
                \Event::dispatch('revisionable.saved', array('model' => $this, 'revisions' => $revisions));
            }
        }
    }
public function postSave ( ) { if ( isset ( $ this -> historyLimit ) && $ this -> revisionHistory ( ) -> count ( ) >= $ this -> historyLimit ) { $ LimitReached = true ; } else { $ LimitReached = false ; } if ( isset ( $ this -> revisionCleanup ) ) { $ RevisionCleanup = $ this -> revisionCleanup ; } else { $ RevisionCleanup = false ; } if ( ( ( ! isset ( $ this -> revisionEnabled ) || $ this -> revisionEnabled ) && $ this -> updating ) && ( ! $ LimitReached || $ RevisionCleanup ) ) { $ changes_to_record = $ this -> changedRevisionableFields ( ) ; $ revisions = array ( ) ; foreach ( $ changes_to_record as $ key => $ change ) { $ original = array ( 'revisionable_type' => $ this -> getMorphClass ( ) , 'revisionable_id' => $ this -> getKey ( ) , 'key' => $ key , 'old_value' => Arr :: get ( $ this -> originalData , $ key ) , 'new_value' => $ this -> updatedData [ $ key ] , 'user_id' => $ this -> getSystemUserId ( ) , 'ip' => $ _SERVER [ " REMOTE_ADDR " ] , 'created_at' => new \ DateTime ( ) , 'updated_at' => new \ DateTime ( ) , ) ; $ revisions [ ] = array_merge ( $ original , $ this -> getAdditionalFields ( ) ) ; } if ( count ( $ revisions ) > 0 ) { if ( $ LimitReached && $ RevisionCleanup ) { $ toDelete = $ this -> revisionHistory ( ) -> orderBy ( 'id' , 'asc' ) -> limit ( count ( $ revisions ) ) -> get ( ) ; foreach ( $ toDelete as $ delete ) { $ delete -> delete ( ) ; } } $ revision = Revisionable :: newModel ( ) ; \ DB :: table ( $ revision -> getTable ( ) ) -> insert ( $ revisions ) ; \ Event :: dispatch ( 'revisionable.saved' , array ( 'model' => $ this , 'revisions' => $ revisions ) ) ; } } }
Called after a model is successfully saved.
Called after a model is successfully saved .
// check if the model already exists // if it does, it means we're updating