Oihana PHP

ExistModel

Contract for models able to test the existence of a document.

An ExistModel answers a single yes/no question: does a document matching the supplied options exist in the underlying storage? It is typically used as a lightweight guard before a heavier read or write, and is the base interface extended by GetModel and LastModel.

Tags
author

Marc Alcaraz (ekameleon)

since
1.0.0

Table of Contents

Methods

exist()  : bool
Indicates whether a document matching the given options exists.

Methods

exist()

Indicates whether a document matching the given options exists.

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

Operation options identifying the document to test, typically a key/id value, a set of conditions and their binds.

Tags
example
use oihana\models\interfaces\ExistModel;

class UserModel implements ExistModel
{
    public function exist( array $init = [] ) : bool
    {
        return isset( $init[ 'key' ] ) && $this->store->has( $init[ 'key' ] ) ;
    }
}

$model = new UserModel() ;

if ( $model->exist( [ 'key' => 'users/42' ] ) )
{
    // safe to fetch or update
}
Return values
bool

true if at least one matching document exists, otherwise false.

On this page

Search results