2016-06-29 26 views
0

使用PHP-ON-COUCH,我嘗試使用php-on-couch獲取couchdb中的所有記錄,但它不能快速運行。php-on-couch快速獲取所有記錄

require_once "lib/couch.php"; 
require_once "lib/couchClient.php"; 
require_once "lib/couchDocument.php";  

$couch_dsn = "http://localhost:5984/"; 
$couch_db = "couch"; 

    $client = new couchClient($couch_dsn,$couch_db); 
    $all_singers = $client->getAllDocs(); 
    foreach ($all_singers->rows as $row) { 
    $doc = $client->getDoc($id); 
    echo $doc->singer; 
    echo $doc->title; 
    echo $doc->description; 
    } 

是否有另一種方法正確地做到這一點?

預先感謝您

回答

0

你沒有正確使用的功能。您現在正在執行的操作極其緩慢,因爲您將所有文檔提取出來,然後使用(getDoc)函數一次一個地讀取它們。當您查詢所有文檔,你得到的東西是這樣的:

{ 
    "total_rows": 0, 
    "count": 0, 
    "rows": [{ 
     "key": 1, 
     "value": "value", 
     "doc": { 
      "singer": "Foo", 
      "title": "bar" 
     } 
    }] 
} 

這是你的代碼的修訂版本:

<?php 

require_once "lib/couch.php"; 
require_once "lib/couchClient.php"; 
require_once "lib/couchDocument.php"; 

$couch_dsn = "http://localhost:5984/"; 
$couch_db = "couch"; 

$client = new couchClient($couch_dsn, $couch_db); 
$all_singers = null; 

try { 
    $all_singers = $client->include_docs(true)->getAllDocs(); 
} catch (Exception $e) { 
    //Handle the exception here. 
} 
if (!isset($all_singers) || !isset($all_singers->rows)) 
    echo "No singers found"; 
else 
    foreach ($all_singers->rows as $row) { 
     if (isset($row->error)) 
      continue; //Log the error or something like this 
     if (isset($row->doc)) { 
      $doc = $row->doc; 
      echo $doc->singer; 
      echo $doc->title; 
      echo $doc->description; 
     } 
    } 
+0

感謝亞歷克西斯,它的作品很好!你只需在類couchClient的方法getAllDocs中添加參數「include_docs」 –

+0

我改變了它,如果它幫助你,請接受答案! :) –