maurymmarques / couchdb-datasource-cakephp
CouchDB 数据源是一个 CakePHP 插件,用于简化 CakePHP 应用程序与 CouchDB 数据库之间的通信。
Requires
- php: >=5.3.0
- composer/installers: *
This package is not auto-updated.
Last update: 2024-09-24 05:20:57 UTC
README
CouchDB 数据源是一种简化 CakePHP 应用程序与 CouchDB 数据库之间通信的方法。
数据源是模型与模型所表示的数据源之间的连接。
CouchDB 是一种主要用 Erlang 编程语言编写的开源文档型数据库。
版本
为 CakePHP 2.x 编写
版权
版权(c)2011 Maury M. Marques
安装
您可以使用 Composer、GIT Submodule、GIT Clone 或手动方式安装此插件。
[使用 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 Submodule]
在您的应用程序目录(app/Plugin)中输入:
git submodule add git://github.com/maurymmarques/couchdb-datasource.git Plugin/CouchDB git submodule init git submodule update
[GIT Clone]
在您的插件目录(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);