2016-04-22 77 views
1

我相信Cloudant最近更改了他們的一些代碼。最近,如果您在try/catch語句中執行了一個storedoc操作。 Cloudant會返回一個「錯誤」的框架:Cloudant和Php-on-Couch由於繼續而不能正常工作

未捕獲的異常「couchException」有消息「繼續

當然,你可以處理它在catch語句,但它確實應該回來的」成功「在PHP-on-Couch庫的Try語句中。

任何人都會遇到這個問題或知道如何處理它?最大的問題是,因爲它的未來作爲一個錯誤,你不能搶在catch語句的ID和Rev:

   try { // does not return here, goes to catch 
        $response = $client->storeDoc($doc); 
        $response_json['status'] = 'success'; 
        $response_json['id'] = $response->id; 
        $response_json['rev'] = $response->rev; 
       } catch (Exception $e) { // even though the doc is successfully storing 

        // check for accepted BEG 
        $error = ''; 
        $error = $e->getMessage(); 
        $err_pos = strpos($error,"Accepted"); 
        $err_pos_2 = strpos($error,"Continue"); 
        if($err_pos !== false OR $err_pos_2 !== false){ // success 

         $response_json['status'] = 'success'; 
         $response_json['id'] = $response->id; // returns null 
         $response_json['rev'] = $response->rev; // returns null 

        } else { // truely an error 

         $response_json['status'] = 'fail'; 
         $response_json['message'] = $e->getMessage(); 
         $response_json['code'] = $e->getCode(); 

        } 
        // check for accepted END 


       } 

回答

0

我在這兩個的CouchDB和Cloudant測試和行爲是一致的。這是我認爲正在發生的事情。當您創建新沙發文檔時:

$doc = new couchDocument($client); 

默認情況下,文檔設置爲自動提交。當你在文檔設置屬性一旦

function __construct(couchClient $client) { 
    $this->__couch_data = new stdClass(); 
    $this->__couch_data->client = $client; 
    $this->__couch_data->fields = new stdClass(); 
    $this->__couch_data->autocommit = true; 
} 

$doc->set(array('name'=>'Smith','firstname'=>'John')); 

storeDoc立即調用您可以在couchDocument.php看到這一點。然後您再次嘗試撥打storeDoc,並且couchDB返回錯誤。

有2種方法來解決這個問題:

  1. 關閉自動提交:

    $doc = new couchDocument($client); 
    $doc->setAutocommit(false); 
    $doc->set(array('name'=>'Smith','firstname'=>'John')); 
    try { 
        $response = $client->storeDoc($doc); 
        $response_json['status'] = 'success'; 
        $response_json['id'] = $response->id; 
        $response_json['rev'] = $response->rev; 
    
  2. 保持自動提交,並得到來自$doc ID和轉後,你設置一個屬性:

    $doc = new couchDocument($client); 
    try { 
        $doc->set(array('name'=>'Smith','firstname'=>'John')); 
        $response_json['status'] = 'success'; 
        $response_json['id'] = $doc->_id; 
        $response_json['rev'] = $doc->_rev; 
    
+0

我試過了bot h方式,帶'message setAutocommit不存在'的未捕獲異常'異常'和類似的$ doc-> set一切工作正常可能幾個星期到一個月前左右。 – Matt

+0

這是你正在使用的庫嗎? https://github.com/dready92/PHP-on-Couch。我剛剛下載了PHP文件,當我回答這個問題時,他們爲我工作。 – markwatsonatx

+0

啊原諒我...我需要使用'couch_document class'...將在稍後測試。感謝您的快速回復和解決方案。 – Matt