Oihana PHP

LastModel extends ExistModel

Contract for models able to fetch their most recent document.

A LastModel extends ExistModel and adds a LastModel::last() operation returning the "latest" document. By default an implementation sorts on the modified property, so last() yields the most recently changed document, but the ordering can be overridden through the 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.
last()  : mixed
Returns the last document in the model.

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.

last()

Returns the last document in the model.

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

Operation options driving the lookup, typically the sort field used to determine "last" (default: the modified property), optional conditions and binds.

Tags
example
use oihana\models\interfaces\LastModel;

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

    public function last( array $init = [] ) : mixed
    {
        return $this->store->latest( $init[ 'sort' ] ?? 'modified' ) ;
    }
}

$model      = new LogModel() ;
$lastEntry  = $model->last() ;                       // by 'modified'
$lastByDate = $model->last( [ 'sort' => 'created' ] ) ;
Return values
mixed

The last matching document, or a null/empty value when the collection is empty, depending on the implementation.

On this page

Search results