Transcoder decoder not working

I am using couchbase server 6.6 and php sdk version 3.0.4. When i test setTranscoder, encoder worked but decoder didn’t work

 18 function encoder($value) {
 19   var_dump($value);
 20   return array($value, SPECIAL_BYTEARRAY, 0);
 21 }
 22 function decoder($bytes, $flags, $datatype) {
 23   var_dump($bytes);
 24   switch ($flags) {
 25     case SPECIAL_INT:
 26       return $bytes;
 27     case SPECIAL_BYTEARRAY;
 28       return $bytes;
 29 
 30   }
 31   return NULL;
 32 }
....
 36 $connectionString = "couchbase://localhost";
 37 $options = new \Couchbase\ClusterOptions();
 38 $options->credentials("Administrator", "112233");
 39 $cluster = new \Couchbase\Cluster($connectionString, $options);
 40 
 41 $bucket = $cluster->bucket("test");
 42 
 43 $bucket->setTranscoder('encoder', 'decoder');

Hello @gio can you please let us know what it would mean when you say didnt work ? Do you see an error ? or decoder is not decoding the data ? What data were you passing ?

It calls function encoder but it doesn’t run into function decoder

Interesting, I am looking at the GitHub code example and it matches to what you have not sure if it has to do with the name of the functions (most unlikely) .

@avsej anything you can think of ?

$bucket->setTranscoder('encoder', 'decoder');
$collection = $bucket->defaultCollection();
$getResult = $collection->get("a");

var_dump($getResult);

echo "result: " . $getResult->content();

It doesn’t invoke function decoder when i call function $getResult-> content()

with recent release 3.1.0 it works as expected:

class MyFlags {
    const SPECIAL_INT = 1;
    const SPECIAL_BYTEARRAY = 2;
}

function encoder($value) {
  if (gettype($value) == "integer") {
    printf("encode value as integer\n");
    return [pack("L", $value), MyFlags::SPECIAL_INT, 0];
  } else {
    printf("encode value as bytearray\n");
    return [base64_encode($value), MyFlags::SPECIAL_BYTEARRAY, 0];
  }
}
function decoder($bytes, $flags, $datatype) {
  if ($flags & MyFlags::SPECIAL_INT) {
      printf("decode value as integer\n");
      return unpack("L", $bytes);
  } else if ($flags & MyFlags::SPECIAL_BYTEARRAY) {
      printf("decode value as bytearray\n");
      return base64_decode($bytes);
  } else {
      printf("unexpected encoding\n");
      return $bytes;
  }
}

$options = new \Couchbase\ClusterOptions();
$options->credentials('Administrator', 'password');
$cluster = new \Couchbase\Cluster('couchbase://localhost/default', $options);
$bucket = $cluster->bucket("default");

$bucket->setTranscoder('encoder', 'decoder');

$collection = $bucket->defaultCollection();
$collection->upsert("foo", 42);
var_dump($collection->get("foo")->content());

Also it is possible to specify encoder/decoder in options to operations (see example below):

Ohh, thank you. I will test with this version