I have the following PHP methods:
function get_latest_doc_id()
{
$myCluster = new CouchbaseCluster('couchbase://localhost');
$myBucket = $myCluster->openBucket();
$myBucket->enableN1ql(array('http://1.1.1.1:8093/','http://1.1.1.2:8093/'));
$query = CouchbaseN1qlQuery::fromString('select meta(`lazycouch`).id from `lazycouch` order by meta(`lazycouch`).id desc limit 1');
$res = $myBucket->query($query);
if ( count($res) > 0 ) {
$id = (int)$res[0]->id;
return $id + 1;
} else {
return 1;
}
}
And
function create_new_audit_record($data)
{
$next_id = (string)get_latest_doc_id();
echo "creating audit record using id " .$next_id."<BR>";
$myCluster = new CouchbaseCluster('couchbase://localhost');
$myBucket = $myCluster->openBucket('lazycouch');
$res = $myBucket->insert($next_id, $data);
return $res;
}
Problem:
The logic to increment the document counter in get_latest_doc_id() is not showing the right value after I do an insert. Here’s the code that drives these two functions, as well as the output:
create_new_audit_record(array('1'=>'test'));
$id = get_latest_doc_id();
echo " after insert, this is next id: $id<BR>";
output:
manually setting count
creating audit record using id 1
manually setting count
after insert, this is next id: 1
I was expecting that after the insert, another call to the get_latest_doc_id would return 2. But it’s as though the insert statement is not committed to the database or something like that because when I refresh my web page, the count does increment properly like so:
creating audit record using id 2
after insert, this is next id: 2
It’s just for the first insert that this is failing…