brebvix / yii2mongo
An extension for using the MongoDB library in a style similar to ActiveRecord.
dev-master
2019-12-19 08:37 UTC
Requires
- php: >=7.2.0
- ext-mongodb: *
- mongodb/mongodb: ^1.5@dev
- yiisoft/yii2: ^2.0.1
This package is auto-updated.
Last update: 2026-02-19 22:31:26 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";