Oihana PHP

UpsertModel

Contract for models able to upsert a document.

An UpsertModel exposes a single UpsertModel::upsert() operation that inserts a document when it does not yet exist, or updates/replaces it when it does. It is the idempotent way to persist a document without first probing for its existence.

Tags
author

Marc Alcaraz (ekameleon)

since
1.0.0

Table of Contents

Methods

upsert()  : mixed
Inserts or updates a document depending on whether it already exists.

Methods

upsert()

Inserts or updates a document depending on whether it already exists.

public upsert([array<string|int, mixed> $init = [] ]) : mixed
Parameters
$init : array<string|int, mixed> = []

Operation options carrying the document and its lookup keys, typically the matching conditions/key, the document payload, optional binds and a return clause.

Tags
example
use oihana\models\interfaces\UpsertModel;

class UserModel implements UpsertModel
{
    public function upsert( array $init = [] ) : mixed
    {
        return $this->store->upsert( $init[ 'key' ] ?? null, $init[ 'document' ] ?? [] ) ;
    }
}

$model = new UserModel() ;
$user  = $model->upsert( [ 'key' => 'users/42', 'document' => [ 'name' => 'Alice' ] ] ) ;
Return values
mixed

The upserted document (or its result), depending on the implementation.

On this page

Search results