PHP client 2.2.0beta3 res = object or array?


#1

Hello.
@avsej

Maybe I’m doing something wrong.
That’s what I have:

php -v
PHP 7.0.7-2 + donate.sury.org ~ trusty + 1 (cli) (NTS)
Couchbase cli 2.2.0beta3

When I do so here:

$query = CouchbaseViewQuery::from('article_by_date', 'date_article_title')->limit($limit)->order(CouchbaseViewQuery::ORDER_DESCENDING);

$res = $bucketArticle->query($query, null, true);

var_dump($res);

I can pass the $res[‘rows’] in other functions.

When I do so here:

$query = CouchbaseViewQuery::from('article_by_date', 'date_article_title')->limit($limit)->order(CouchbaseViewQuery::ORDER_DESCENDING);

$res = $bucketArticle->query($query);

And I try to pass the $res[‘rows’] to another function

So I get an error:

Fatal error: Uncaught Error: Cannot use object of type stdClass as array in /var/www/html/index.php

Difference - , null, true

But var_dump($res); working anyway.

Couchbase cli I installed using:

apt-get install php7.0-dev
pear config-set php_ini /etc/php/7.0/apache2/php.ini
pecl config-set php_ini /etc/php/7.0/apache2/php.ini
pecl install couchbase-2.2.0beta3
service apache2 restart

#2

it depends how the other function accesses the results? I guess in your case it is treating it as array. The third argument true forces conversion of results into nested arrays, so it is okay to access it as array.

$res['rows']

But by default this argument is false and it will use stdClass instead, which have to be accessed like this

$res->rows

#3

in case of query($query, null, true);

I get:

array(2) {
  ["total_rows"]=>
  int(592)
  ["rows"]=>

in case of query($query);

I get:

object(stdClass)#10 (2) {
  ["total_rows"]=>
  int(592)
  ["rows"]=>

Magic.


#4

not magic, just defaults. also checkout this code:

echo("default JSON decoder options: ");
var_dump($COUCHBASE_DEFAULT_DECOPTS);

$res = $bucket->get('foo');
var_dump($res->value);

echo("setting 'jsonassoc' to true\n");
$COUCHBASE_DEFAULT_DECOPTS['jsonassoc'] = true;

$res = $bucket->get('foo');
var_dump($res->value);

Output:

default JSON decoder options: array(1) {
  ["jsonassoc"]=>
  bool(false)
}
object(stdClass)#4 (1) {
  ["email"]=>
  string(11) "foo@foo.com"
}
setting 'jsonassoc' to true
array(1) {
  ["email"]=>
  string(11) "foo@foo.com"
}

#5

You mean that’s the case:

In other words, my question is:

What impact has the addition null, true to the function query($query, null, true)?

Thank you.


#6

these two unrelated questions, the one you are referring is related to broken flags, which used to identify the content type (and apply corresponding decoder).

third argument is related to the way how standard JSON decoder represents result. You can read more here about it: http://php.net/json_decode

assoc
When TRUE, returned objects will be converted into associative arrays.


#7

Thank you.

Now I understand everything.

I used to use arrays.

Before, when I used the version 2.0 as an answer I always got an array.

Something has changed between version 2.0 and 2.2?


#8

@avsej One more question. What does the option: null

into:

query($query, null, true);


#9

It represents query parameters


#10

Now I understand everything.
Previously, I used an older version of the Couchbase php client.
The query result is always returned array.
Now the query always returns an object.

Here’s the story of my work and errors:

$query = CouchbaseViewQuery::from('article_by_date', 'date_article_title')->limit($limit)->order(CouchbaseViewQuery::ORDER_DESCENDING);
$res = $bucketArticle->query($query);

var_dump($res);

php 7.0
cb cli 2.2-beta3:

object(stdClass)#10 (2) {
  ["total_rows"]=>
  int(592)
  ["rows"]=>
  array(16) {

php 5
cb cli 2.1.0:

<br />
<b>Fatal error</b>:  Cannot use object of type stdClass as array in <b>[CouchbaseNative]/CouchbaseBucket.class.php</b> on line <b>284</b><br />

php 5
cb cli 2.0.7

array(2) {
  ["total_rows"]=>
  int(592)
  ["rows"]=>
  array(16) {