couchdb-odm
couchdb-odm copied to clipboard
Null _rev passed to CouchDB
We tried mapping the CouchDB _rev string to a PHP property. The line we used in the XML mapping file was:
<field name="etag" type="string" version="true" json-name="_rev"></field>
This works when fetching a document from the database, but it fails when trying to persist a new document to the database.
The problem is in Doctrine\ODM\CouchDB\UnitOfWork::flush()
where the field values are converted to JSON values. The _rev version field is included in the JSON, even if it is null. This results in a 400 bad request response from CouchDB.
Our workaround is to replace line 1072 with the following code snippet:
$jsonName = $class->fieldMappings[$fieldName]['jsonName'];
if( $jsonName != '_rev' || $fieldValue ) $data[$jsonName] = $fieldValue;
Rather than testing the JSON name, it might be more consistent with the rest of your design to test $class->fieldMappings[$fieldName]['isVersionField']
or to check whether $fieldName
is equal to $class->versionField
.