doctrine / couchdb
此包已被废弃且不再维护。未建议替代包。
CouchDB 客户端
2.0.0-alpha1
2018-05-07 00:03 UTC
Requires
- php: >=5.4
Requires (Dev)
- phpunit/phpunit: ~4.0
This package is auto-updated.
Last update: 2024-02-29 02:27:35 UTC
README
一个简单的API,它封装了CouchDB v2.x HTTP API。
对于CouchDB 1.x,请检查我们的 release/1.0.0 分支。
特性
- 创建、删除、列出数据库
- 创建、更新、删除文档
- 批量API用于创建/更新文档
- 通过ID查找文档
- 生成UUIDs
- 设计文档
- 查询
_all_docs
视图 - 查询更改流
- 查询视图
- 压缩信息及触发API
- 复制API
- Symfony控制台命令
- 使用Mango查询查找文档
安装
使用Composer
{
"require": {
"doctrine/couchdb": "@dev"
}
}
使用方法
基本操作
涵盖数据库和文档的基本CRUD操作
<?php $client = \Doctrine\CouchDB\CouchDBClient::create(array('dbname' => 'doctrine_example')); // Create a database. $client->createDatabase($client->getDatabase()); // Create a new document. list($id, $rev) = $client->postDocument(array('foo' => 'bar')); // Update a existing document. This will increment the revision. list($id, $rev) = $client->putDocument(array('foo' => 'baz'), $id, $rev); // Fetch single document by id. $doc = $client->findDocument($id); // Fetch multiple documents at once. $docs = $client->findDocuments(array($id)); // Return all documents from database (_all_docs?include_docs=true). $allDocs = $client->allDocs(); // Delete a single document. $client->deleteDocument($id, $rev); // Delete a database. $client->deleteDatabase($client->getDatabase()); //Search documents using Mango Query CouchDB v2.x $selector = ['_id'=>['$gt'=>null]]; $options = ['limit'=>1,'skip'=>1,'use_index'=>['_design/doc','index'],'sort'=>[['_id'=>'desc']]]; $query = new \Doctrine\CouchDB\Mango\MangoQuery($selector,$options); $docs = $client->find($query); $query = new \Doctrine\CouchDB\Mango\MangoQuery(); $query->select(['_id', 'name'])->where(['$and'=> [ [ 'name'=> [ '$eq'=> 'Under the Dome', ], 'genres'=> [ '$in'=> ['Drama','Comedy'], ], ], ])->sort([['_id'=>'desc']])->limit(1)->skip(1)->use_index(['_design/doc','index']); $docs = $client->find($query);
视图
一个简单示例,展示如何创建视图并查询它们
<?php class ArticlesDesignDocument implements \Doctrine\CouchDB\View\DesignDocument { public function getData() { return array( 'language' => 'javascript', 'views' => array( 'by_author' => array( 'map' => 'function(doc) { if(\'article\' == doc.type) { emit(doc.author, doc._id); } }', 'reduce' => '_count' ), ), ); } } $client->createDesignDocument('articles', new ArticlesDesignDocument()); // Fill database with some data. foreach (array('Alice', 'Bob', 'Bob') as $author) { $client->postDocument(array( 'type' => 'article', 'author' => $author, 'content' => 'Lorem ipsum' )); } // Query all articles. $query = $client->createViewQuery('articles', 'by_author'); $query->setReduce(false); $query->setIncludeDocs(true); $result = $query->execute(); foreach ($result as $row) { $doc = $row['doc']; echo 'Article by ', $doc['author'], ': ', $doc['content'], "\n"; } // Article by Alice: Lorem ipsum // Article by Bob: Lorem ipsum // Article by Bob: Lorem ipsum // Query all articles written by bob. $query = $client->createViewQuery('articles', 'by_author'); $query->setKey('Bob'); // ... // Query the _count of articles each author has written. $query = $client->createViewQuery('articles', 'by_author'); $query->setReduce(true); $query->setGroupLevel(1); // group_level=1 means grouping by author. $result = $query->execute(); foreach ($result as $row) { echo 'Author ', $row['key'], ' has written ', $row['value'], ' articles', "\n"; } // Author Alice has written 1 articles // Author Bob has written 2 articles