nacer/mongodbcakephp

CakePHP 的 MongoDB 数据源

安装: 5

依赖: 0

建议者: 0

安全: 0

星标: 0

关注者: 1

分支: 0

开放问题: 0

类型:cakephp-plugin

dev-master 2019-03-20 11:21 UTC

This package is auto-updated.

Last update: 2024-09-21 00:24:27 UTC


README

通过 composer 安装

安装 composer 并运行

composer require nacer/mongodbcakephp dev-master

启用插件

// open src/Application.php
// then add plugin like below

public function bootstrap()
{
    $this->addPlugin('Nacer/Mongodbcakephp');
}

在 CakePHP 3.7 之前 (3.5.* & 3.6.*)

// at the end of this file config/bootstrap.php, add the code bellow

Plugin::load('Nacer/Mongodbcakephp');

使用 MongoDB 认证信息填写默认数据源

// In config/app.php file, replace the default datasource using mongodb like below
 'Datasources' => [
    'default' => [
        'className' => 'Nacer\Mongodbcakephp\Database\Connection',
        'driver' => 'Nacer\Mongodbcakephp\Database\Driver\Mongodb',
        'persistent' => false,
        'host' => 'localhost',
        'port' => 27017,
        'login' => '',
        'password' => '',
        'database' => 'devmongo',
        'ssh_host' => '',
        'ssh_port' => 22,
        'ssh_user' => '',
        'ssh_password' => '',
        'ssh_pubkey_path' => '',
        'ssh_privatekey_path' => '',
        'ssh_pubkey_passphrase' => ''
    ],
],

SSH 隧道变量(以 'ssh_' 开头)

如果您想通过 SSH 隧道连接到 MongoDB,您需要在数据源中设置额外的变量。根据您的连接方式,一些变量可能是不必要的。如果您使用 SSH 密钥文件连接,则需要 ssh_pubkey_pathssh_privatekey_path 变量,而 ssh_password 变量是不必要的。如果您使用基于文本的密码(这 不是 一个好主意)连接,则相反。该函数至少需要 ssh_hostssh_user 和一种认证方法来建立 SSH 隧道。

模型

之后,您需要在您的表类中加载 Nacer\Mongodbcakephp\ORM\Table

//src/Model/Table/YourTable.php

use Nacer\Mongodbcakephp\ORM\Table;

class CategoriesTable extends Table {

}

观察

函数 find() 只在传统方式下工作。所以,如果您想找到某些东西,您需要像以下示例一样操作

$this->Categories->find('all', ['conditions' => ['name' => 'teste']]);
$this->Categories->find('all', ['conditions' => ['name LIKE' => 'teste']]);
$this->Categories->find('all', ['conditions' => ['name' => 'teste'], 'limit' => 3]);

您也可以使用 MongoDB 的高级条件,使用 MongoDB\BSON 命名空间

$this->Categories->find('all', ['conditions' => [
    '_id' => new \MongoDB\BSON\ObjectId('5a7861909db0b47d605c3865'),
    'foo.bar' => new \MongoDB\BSON\Regex('^(foo|bar)?baz$', 'i')
]]);

许可

MIT 许可证 (MIT) 版权所有 (c) 2013