mofing / mongodb
CakePHP 3.0 的 Mongodb 数据源
1.0.1
2019-03-11 06:26 UTC
Requires
- php: ^5.4|^7.0
- cakephp/cakephp: ^3.5
- mongodb/mongodb: ^1.2
Requires (Dev)
- phpunit/phpunit: ^6.0
This package is auto-updated.
Last update: 2024-09-12 04:34:10 UTC
README
Cakephp3的Mongodb
CakePHP 3.5 的 Mongodb 数据源
通过 composer 安装
安装 composer 并运行
composer require mofing/mongodb
将插件连接到您的应用
在您的 config/bootstrap.php 中添加以下行以告诉您的应用加载插件
Plugin::load('Mofing/Mongodb');
定义连接
现在,您需要在 config/app.php 文件中设置连接
'Datasources' => [ 'default' => [ 'className' => 'Mofing\Mongodb\Database\Connection', 'driver' => 'Mofing\Mongodb\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_path
和 ssh_privatekey_path
变量,而 ssh_password
变量是不必要的。如果您使用基于文本的密码(这不是一个好主意),则相反。函数至少需要 ssh_host
、ssh_user
和一种建立 SSH 隧道的认证方法。
模型
之后,您需要在您的 tables 类中加载 Hayko\Mongodb\ORM\Table
//src/Model/Table/YourTable.php use Mofing\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') ]]);