brebvix/yii2mongo

An extension for using the MongoDB library in a style similar to ActiveRecord.

dev-master 2019-12-19 08:37 UTC

This package is auto-updated.

Last update: 2024-04-19 18:16:20 UTC


README

An extension for using the MongoDB library in a style similar to ActiveRecord.

Installation

composer require brebvix/yii2mongo

Add to the configuration file params-local.php:

'mongo' => [
    'connectionUrl' => 'mongodb://<username>:<password>@<host>:<port>',
    'databaseName' => '<database_name>',
],

Documentation: https://docs.mongodb.com/php-library/current/tutorial/

Example

Model class

<?php
use brebvix\Mongo;

class UserModel extends Mongo
{
    /**
    * @return string
    */
    public static function collectionName(): string
    {
        return 'users';
    }
    
    //Usage in model:
    
    public static function getAuthorizedUsersCount(): int
    {
        return self::count([
            'authorized' => true
        ]);
    }
}

Use outside the model:

<?php
$count = UserModel::getAuthorizedUsersCount();
echo "Authorized users count: $count";

// OR

$count = UserModel::count([
    'authorized' => true
]);
echo "Authorized users count: $count";