Oihana PHP

CountModel

Contract for models able to count the documents they manage.

A CountModel exposes a single CountModel::count() operation that returns how many documents match the supplied options, without materializing the documents themselves. It is the cheapest way to probe the size of a collection or the cardinality of a filtered query.

Tags
author

Marc Alcaraz (ekameleon)

since
1.0.0

Table of Contents

Methods

count()  : int
Returns the number of documents matching the given options.

Methods

count()

Returns the number of documents matching the given options.

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

Operation options driving the count, e.g. filtering conditions, bind variables (binds) or a precompiled query. An empty array counts every document.

Tags
example
use oihana\models\interfaces\CountModel;

class UserModel implements CountModel
{
    public function count( array $init = [] ) : int
    {
        // run a COUNT query using $init['conditions'] / $init['binds']
        return 128 ;
    }
}

$model = new UserModel() ;

$total  = $model->count() ;                                  // every user
$active = $model->count( [ 'conditions' => 'doc.active == true' ] ) ;
Return values
int

The number of matching documents (0 when none match).

On this page

Search results