Service

A more in-depth look at the MondocAbstractService, which is the extension source for all database services in Mondoc.

About the MondocAbstractService

The MondocAbstractService provides the base class for all services in Mondoc. The service represents a MongoDB collection, and the class is responsible for all CRUD operations on that collection.

Minimal service methods

The following methods should be implemented in your service classes:

  • getCollectionName(): string IMPLEMENT
    Returns the name of the MongoDB collection. This method is required to be implemented in your service class.
    You can override this at runtime by implementing the getMondocCollectionNameOverride method within your model.
  • getConnectionId(): string OPTIONAL
    Return the connectionId to use for this service. This is particularly useful if you want separation between data, for example if your application requires one database connection for users, and another for the application data.
    The default value is default. The connectionId correlates to the connectionId set when assigning the database with setDatabase in MondocConfig.
    Read more in the Connections section.
    You can override this at runtime by implementing the getMondocConnectionIdOverride method within your model.

Exposed methods

The most commonly used methods in the MondocAbstractService class are below. You can view the full list of methods in the GitHub repositoryopen_in_new.

  • getMultiByCriteria(array $filter = [], array $options = []): array
    Returns an array of models that match the criteria. The criteria is an array of key-value pairs that are used to filter the results.
  • getMultiByQueryBuilder(\District5\MondocBuilder\QueryBuilder $builder): array
    Returns an array of models that match the query builder. The query builder is an instance of \District5\MondocBuilder\QueryBuilder that is used to filter the results.
  • getByIds(array $ids): array
    Returns an array of models that match the provided IDs. The IDs are an array of ObjectId or string values.
  • getMultiWhereKeyEqualsValue(string $key, mixed $value): array
    Returns an array of models where a specific key equals a specific value.
  • getMultiWhereKeyDoesNotEqualValue(string $key, mixed $value): array
    Returns an array of models where a specific key does not equal a specific value.
  • getOneByCriteria(array $filter = [], array $options = []): ?\District5\Mondoc\Db\Model\MondocAbstractModel
    Returns a single model that matches the criteria. The criteria is an array of key-value pairs that are used to filter the results.
  • getByIds(array $ids): array
    Returns a single model that matches the provided ID. The ID is an ObjectId or string value.
  • getOneByQueryBuilder(\District5\MondocBuilder\QueryBuilder $builder): ?\District5\Mondoc\Db\Model\MondocAbstractModel
    Returns a single model that matches the query builder. The query builder is an instance of \District5\MondocBuilder\QueryBuilder that is used to filter the results.
  • getOneWhereKeyEqualsValue(string $key, mixed $value): ?\District5\Mondoc\Db\Model\MondocAbstractModel
    Returns a single model where a specific key equals a specific value.
  • getOneWhereKeyDoesNotEqualValue(string $key, mixed $value): ?\District5\Mondoc\Db\Model\MondocAbstractModel
    Returns a single model where a specific key does not equal a specific value.
  • getPaginationHelper(int $currentPageNumber, int $perPage, array $filter = []): \District5\Mondoc\Helper\MondocPaginationHelper
    Returns a pagination helper instance that is used to paginate the results. The method accepts the current page, the number of items per page, and a query array.
  • getPage(\District5\Mondoc\Helper\MondocPaginationHelper $paginator, ?string $sortByField = '_id', int $sortDirection = -1): array
    Returns an array of models that match the pagination helper. The pagination helper is an instance of \District5\Mondoc\Helper\MondocPaginationHelper that is used to filter the results.
  • getPageByByObjectIdPagination(\District5\Mondoc\Helper\MondocPaginationHelper $paginator, \MongoDB\BSON\ObjectId|string|null $currentId, int $sortDirection = -1): array
    Returns an array of models that match the pagination helper and the current ID. The pagination helper is an instance of \District5\Mondoc\Helper\MondocPaginationHelper that is used to filter the results. The current ID is the current document ID.
  • countAll(array $filter = [], array $options = []): int
    Returns the total number of documents matching a given query.
  • countAllByQueryBuilder(\District5\MondocBuilder\QueryBuilder $builder): int
    Returns the total number of documents matching a given query builder. The query builder is an instance of \District5\MondocBuilder\QueryBuilder that is used to filter the results.
  • countWhereKeyEqualsValue(string $key, mixed $value): int
    Returns the total number of documents where a specific key equals a specific value.

Full service example

Below is an example of a service class that extends the MondocAbstractService.

<?php

use District5\Mondoc\Db\Service\MondocAbstractService;

use MyModel;

/**
 * @method static MyModel[] getMultiByCriteria(array $filter = [], array $options = [])
 * @method static MyModel[] getMultiByQueryBuilder(\District5\MondocBuilder\QueryBuilder $builder)
 * @method static MyModel[] getByIds(array $ids)
 * @method static MyModel[] getMultiWhereKeyEqualsValue(string $key, $value)
 * @method static MyModel[] getMultiWhereKeyDoesNotEqualValue(string $key, $value)
 * @method static MyModel|null getOneByCriteria(array $filter = [], array $options = [])
 * @method static MyModel|null getById(\MongoDB\BSON\ObjectId|string $id)
 * @method static MyModel|null getOneByQueryBuilder(\District5\MondocBuilder\QueryBuilder $builder)
 * @method static MyModel|null getOneWhereKeyEqualsValue(string $key, mixed $value)
 * @method static MyModel|null getOneWhereKeyDoesNotEqualValue(string $key, mixed $value)
 * @method static MyModel[] getPage(\District5\Mondoc\Helper\MondocPaginationHelper $paginator, ?string $sortByField = '_id', int $sortDirection = -1): array
 * @method static MyModel[] getPageByByObjectIdPagination(\District5\Mondoc\Helper\MondocPaginationHelper $paginator, \MongoDB\BSON\ObjectId|string|null $currentId, int $sortDirection = -1)
 */
class MyService extends MondocAbstractService
{
    protected static function getCollectionName(): string
    {
        return 'my_collection'; // This is the MongoDB collection name in the database
    }

    protected static function getConnectionId(): string
    {
        return 'default'; // This is the connection ID, corresponding to the connection in the config file
    }
}

The above example also contains some commonly used @method annotations for easier code completion and type hinting.

View all @method annotationscode