2016-04-22 75 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