shockraver/mongodb

为 CakePHP 3 和 4 提供的 Mongodb 数据源

安装次数: 10,138

依赖者: 0

建议者: 0

安全: 0

星标: 0

关注者: 0

分支: 29

类型:cakephp-plugin

1.2 2024-01-04 12:49 UTC

This package is auto-updated.

Last update: 2024-09-04 14:19:08 UTC


README

Slack

Cakephp 3 和 4 的 Mongodb

为 CakePHP 3 和 4 提供的 Mongodb 数据源

通过 composer 安装

安装 composer 并运行

composer require shockraver/mongodb

将插件连接到您的应用程序

在您的 config/bootstrap.php 中添加以下行以告诉您的应用程序加载插件

Plugin::load('Giginc/Mongodb');

定义连接

现在,您需要在 config/app.php 文件中设置连接

 'Datasources' => [
    'default' => [
        'className' => 'Giginc\Mongodb\Database\Connection',
        'driver' => 'Giginc\Mongodb\Database\Driver\Mongodb',
        'persistent' => false,
        'host' => 'localhost',
        'port' => 27017,
        'username' => '',
        '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 隧道的身份验证方法。

模型

之后,您需要在您的 tables 类中加载 Giginc\Mongodb\ORM\Table

//src/Model/Table/YourTable.php

use Giginc\Mongodb\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