brebvix / yii2mongo
一个扩展,用于以类似 ActiveRecord 的方式使用 MongoDB 库。
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: 2024-09-19 19:11:06 UTC
README
一个扩展,用于以类似 ActiveRecord 的方式使用 MongoDB 库。
安装
composer require brebvix/yii2mongo
将以下内容添加到配置文件 params-local.php
'mongo' => [ 'connectionUrl' => 'mongodb://<username>:<password>@<host>:<port>', 'databaseName' => '<database_name>', ],
文档: https://docs.mongodb.com/php-library/current/tutorial/
示例
模型类
<?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 ]); } }
在模型外部使用
<?php $count = UserModel::getAuthorizedUsersCount(); echo "Authorized users count: $count"; // OR $count = UserModel::count([ 'authorized' => true ]); echo "Authorized users count: $count";