Oihana PHP

GetModel extends ExistModel

Contract for models able to retrieve a single document.

A GetModel extends ExistModel, so an implementation can both test for a document and fetch it. The GetModel::get() operation returns the resolved document (or value) identified by the supplied options.

Tags
author

Marc Alcaraz (ekameleon)

since
1.0.0

Table of Contents

Methods

exist()  : bool
Indicates whether a document matching the given options exists.
get()  : mixed
Returns a single document matching the given options.

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.

get()

Returns a single document matching the given options.

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

Operation options identifying the document to fetch, typically a key/id value, optional conditions with their binds and projection settings.

Tags
example
use oihana\models\interfaces\GetModel;

class UserModel implements GetModel
{
    public function exist( array $init = [] ) : bool { return true ; }

    public function get( array $init = [] ) : mixed
    {
        return $this->store->find( $init[ 'key' ] ?? null ) ;
    }
}

$model = new UserModel() ;
$user  = $model->get( [ 'key' => 'users/42' ] ) ;
Return values
mixed

The resolved document (commonly an array or object), or a null/empty value when nothing matches, depending on the implementation.

On this page

Search results