maurymmarques / couchdb-datasource-plugin
CouchDB 数据源是一个 CakePHP 插件,用于简化 CakePHP 应用程序与 CouchDB 数据库之间的通信。
Requires
- php: >=5.3.0
- composer/installers: *
This package is not auto-updated.
Last update: 2024-09-18 07:20:55 UTC
README
CouchDB 数据源是 CakePHP 应用程序与 CouchDB 数据库之间通信的一种方式。
数据源是模型与其表示的数据源之间的链接。
CouchDB 是一个主要用 Erlang 编程语言编写的开源文档型数据库。
版本
适用于 CakePHP 2.x
版权
版权 (c) 2011 Maury M. Marques
安装
您可以使用 Composer、GIT 子模块、GIT 克隆或手动安装此插件。
[使用 Composer]
将插件添加到项目的 composer.json
文件中 - 略如以下示例
{ "require": { "maurymmarques/couchdb-datasource-plugin": "dev-master" }, "extra": { "installer-paths": { "app/Plugin/CouchDB": ["maurymmarques/couchdb-datasource-plugin"] } } }
然后只需运行 composer install
因为此插件在其自己的 composer.json
中设置了类型 cakephp-plugin
,所以 composer 会知道将其安装到您的 /Plugin
目录中,而不是通常的 vendors 文件。
[GIT 子模块]
在您的应用目录(app/Plugin
)中键入
git submodule add git://github.com/maurymmarques/couchdb-datasource.git Plugin/CouchDB git submodule init git submodule update
[GIT 克隆]
在您的插件目录(app/Plugin
或 plugins
)中键入
git clone https://github.com/maurymmarques/couchdb-datasource.git CouchDB
[手动]
- 下载 CouchDB 存档。
- 解压下载的文件。
- 将得到的文件夹重命名为
CouchDB
- 然后将此文件夹复制到
app/Plugin/
或plugins
配置
在 app/Config/bootstrap.php 中启动插件
CakePlugin::load('CouchDB');
在 app/Config/database.php 中配置连接
class DATABASE_CONFIG { public $default = array( 'datasource' => 'CouchDB.CouchDBSource', 'persistent' => false, 'host' => 'localhost', 'port' => '5984', 'login' => 'root', 'password' => 'root', 'database' => null, 'prefix' => '' ); }
使用方法
数据源基本上与 CakePHP 的工作方式相同
创建模型
class Post extends AppModel { public $schema = array( 'title' => array( 'type' => 'string', 'null' => true, 'key' => 'primary', 'length' => 32 ) ); }
您可以使用属性 Model::useTable
在模型中设置另一个 CouchDB 数据库名称
public $useTable = 'posts';
保存文档
$data = array('title' => 'My new title'); $this->Post->save($data); // Id $this->Post->id; // Revision $this->Post->rev;
搜索文档
$conditions = array('Post.id' => $this->Post->id); $result = $this->Post->find('first', compact('conditions'));
按特定修订版搜索文档
$conditions = array('Post.id' => $this->Post->id, 'Post.rev' => $this->Post->rev); $result = $this->Post->find('first', compact('conditions'));
更改文档(更改最后修订版)
$data = array('title' => 'My new title'); $this->Post->id = '8e64f1eadab2b3b32c94ef2scf3094420'; $this->Post->save($data);
将文档更改到特定修订版
$data = array('title' => 'My new title'); $this->Post->id = '8e64f1eadab2b3b32c94ef2scf3094420'; $this->Post->rev = '26-5cd5713759905feeee9b384edc4cfb61'; $this->Post->save($data);
删除文档
$this->Post->id = '8e64f1eadab2b3b32c94ef2scf3094420'; $this->Post->delete($data);
REST
您可以使用以下方法:curlGet、curlPost、curlPut、curlDelete
$post = array( 'source' => 'post', 'target' => 'post-replicate', 'countinuos' => true ); $return = $this->Post->curlPost('_replicate', $post, true, false);