Couchbase
  • Why NoSQL?
  • Couchbase Server
  • Download
  • Resources
  • Careers
Home | Forums | SDKs | SDKs

PHP Append Functionality Broke

2 replies [Last post]
  • Login or register to post comments
Tue, 03/05/2013 - 13:56
jgillmanjr
Offline
Joined: 03/05/2013
Groups: None

Greetings,

So it looks like append functionality is broke with the PHP API.

This works:

<?php
        $cb = new Couchbase("couchbase1:8091", "", "", "default");
 
        $array['test1'] = array("One", "Two", "Three");
        $array['test2'] = array("A", "B", "C");
        $array = json_encode($array);
 
        $cb->set("test", $array);
        $result = json_decode($cb->get("test"), TRUE);
        print_r($result);
 
/*
        $updated['testappend'] = array("Append 1", "Append 2");
        $updated = json_encode($updated);
 
        $cb->append("test", $updated);
        $result = json_decode($cb->get("test"), TRUE);
        print_r($result);
*/
?>

When viewing the document in the web interface, all is good:

{
  "test1": [
    "One",
    "Two",
    "Three"
  ],
  "test2": [
    "A",
    "B",
    "C"
  ]
}

However, when I uncomment the append section and run the script:

<?php
        $cb = new Couchbase("couchbase1:8091", "", "", "default");
 
        $array['test1'] = array("One", "Two", "Three");
        $array['test2'] = array("A", "B", "C");
        $array = json_encode($array);
 
        $cb->set("test", $array);
        $result = json_decode($cb->get("test"), TRUE);
        print_r($result);
 
 
        $updated['testappend'] = array("Append 1", "Append 2");
        $updated = json_encode($updated);
 
        $cb->append("test", $updated);
        $result = json_decode($cb->get("test"), TRUE);
        print_r($result);
 
?>

This is what I get when I look at the document now:

"eyJ0ZXN0MSI6WyJPbmUiLCJUd28iLCJUaHJlZSJdLCJ0ZXN0MiI6WyJBIiwiQiIsIkMiXX17InRlc3RhcHBlbmQiOlsiQXBwZW5kIDEiLCJBcHBlbmQgMiJdfQ=="

Thoughts? I am just starting with Couchbase, is there something I'm missing, or is the method indeed broke?

Thanks!

-Jason

Top
  • Login or register to post comments
Thu, 03/07/2013 - 07:25
tgrall
Online
Joined: 09/05/2012
Groups: None

Hello,

This is due to the fact that the "appen()" operation has nothing to do with JSON.
It allows you to append "bytes" at the end of the "bytes" that are store in Couchbase.

As you may know, you can store anything into Couchbase : binary, text, numbers,.. and JSON,and the append/prepend is just done on the value not the "JSON".

If you look at the String (do not json_decode) you will see something like

{
  "test1": [
    "One",
    "Two",
    "Three"
  ],
  "test2": [
    "A",
    "B",
    "C"
  ]
}
{
"Append 1", "Append 2
}

if you want to append a JSON array you have to use JSON in your code and do a set/replace into Couchbase.

Take a look to this sample code:
https://github.com/couchbaselabs/DeveloperDay/blob/master/PHP/06_nonjson...

Regards

__________________

Tug
@tgrall

Top
  • Login or register to post comments
Thu, 03/07/2013 - 11:52
jgillmanjr
Offline
Joined: 03/05/2013
Groups: None

Tug,

Thanks for the information!

-Jason

Top
  • Login or register to post comments
  • Login or register to post comments
  • Login
  • Register

Company

  • About Us
  • Leadership
  • Customers
  • Partners
  • Contact Us

Product

  • Couchbase Server
  • Couchbase SDKs
  • Use Cases
  • Documentation
  • Forums

Open Source

  • Couchbase Project
  • Couchbase vs. CouchDB

Commercial

  • Subscriptions & Support
  • Training & Services

News

  • Blog
  • Newsletter
  • Press Releases
  • Buzz

Follow Us

    
  • Customer Login
  • Terms of Service
  • Privacy Policy
  • Trademark Policy
  • Site Map

© 2013 COUCHBASE All rights reserved.

Sign in to Couchbase Community

close
  • Create new account
  • Request new password
You are logging into the Forums, Wiki and Issue Tracker