src/Titon/Db/Repository.php
public function upsert($data, $id = null, array $options = []) {
        $pk = $this->getPrimaryKey();
        $update = false;

        // Check for an ID in the data
        if (!$id && isset($data[$pk])) {
            $id = $data[$pk];
        }

        unset($data[$pk]);

        // Check for record existence
        if ($id) {
            $update = $this->exists($id);
        }

        // Either update
        if ($update) {
            if ($this->update($id, $data, $options) === false) {
                return 0;
            }

        // Or insert
        } else {
            $id = $this->create($data, $options);
        }

        return $id;
    }
public function upsert($data, $id = null, array $options = []) {
        $pk = $this->getPrimaryKey();
        $update = false;

        // Check for an ID in the data
        if (!$id && isset($data[$pk])) {
            $id = $data[$pk];
        }

        unset($data[$pk]);

        // Check for record existence
        if ($id) {
            $update = $this->exists($id);
        }

        // Either update
        if ($update) {
            if ($this->update($id, $data, $options) === false) {
                return 0;
            }

        // Or insert
        } else {
            $id = $this->create($data, $options);
        }

        return $id;
    }
public function upsert ( $ data , $ id = null , array $ options = [ ] ) { $ pk = $ this -> getPrimaryKey ( ) ; $ update = false ; // Check for an ID in the data if ( ! $ id && isset ( $ data [ $ pk ] ) ) { $ id = $ data [ $ pk ] ; } unset ( $ data [ $ pk ] ) ; // Check for record existence if ( $ id ) { $ update = $ this -> exists ( $ id ) ; } // Either update if ( $ update ) { if ( $ this -> update ( $ id , $ data , $ options ) === false ) { return 0 ; } // Or insert } else { $ id = $ this -> create ( $ data , $ options ) ; } return $ id ; }
Either update or insert a record by checking for ID and record existence.

@param array|\Titon\Type\Contract\Arrayable $data
@param int $id
@param array $options
@return int The record ID on success, 0 on failure
Either update or insert a record by checking for ID and record existence .