我相信Cloudant最近改变了他们的一些代码。 最近,如果您在 try/catch 语句中执行 storageoc 操作。Cloudant 将向框架返回一个"错误":
未捕获的异常"沙发异常",消息为"继续"
当然,你可以在 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
}
我在CouchDB和Cloudant中都进行了测试,行为是相同的。这就是我认为正在发生的事情。创建新的沙发文档时:
$doc = new couchDocument($client);
默认情况下,文档设置为自动提交。你可以在couchDocument.php中看到这一点:
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
立即被召唤。然后,您尝试再次调用storeDoc
,couchDB 返回错误。
有两种方法可以解决此问题:
关闭自动提交:
$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;
保持自动提交打开状态,并在设置属性后从
$doc
获取 id 和 rev:$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;