dready92 / PHP-on-Couch

Data Access Library to access a CouchDB server with PHP.
http://dready.byethost31.com/index.php/display/view/192
GNU Lesser General Public License v3.0
246 stars 102 forks source link

problems with getting a proper response from the coauchDB due to submitting the key the right way... #19

Closed SMut closed 13 years ago

SMut commented 13 years ago

Description of the view I created: _id for the document created this way: "_id": "verband/regional/landes/kreis/staffel" so, there is the possibility that an _id looks like this: "_id": "verband/staffel" So I created a view using this RegEx: /^[a-z/]+(?![A-Za-z0-9-]+)/ which matches lower-keys and a slash until an expression having defenitely no slash. If I take a look at Futon running the view I get a response and the key section's looking fine apart of the fact that it seems to be an array - why that? ["verband/regional/landes/kreis"] So, if I directly put this to an URL with the key wrapped like this: ?key=["verband/regional/landes/kreis"] the browser shows me what I want and the log shows me this:

[Sun, 09 Jan 2011 09:32:11 GMT] [info] [<0.20686.49>] 88.130.208.94 - - 'GET' /staffel(2010-2011)/_design/show/_view/like?key=[%22dhb/shv/baden/karlsruhe%22] 304

complete view script: function(doc) { if(doc._id) { // from the start (^) match everything that has lower case characters and a slash // as many times possible [a-z]+ until finding a match without a slash which is dropped // (?![A-Za-z0-9-]+) var prefix = doc._id.match(/^[a-z/]+(?![A-Za-z0-9-]+)/); if(prefix) { emit(prefix,doc); } } }

SMut commented 13 years ago

Now I tried and tried and the problem are the brackets around the value of 'prefix'. So, if you got some time to kill, you can try this view: { function(doc) { if(doc._id) { var prefix = doc._id.match(/^[a-z/]+(?![A-Za-z0-9-]+)/); if(prefix) { emit(prefix,doc); } } } }

If you have some documents and take a peek of the view in futon you get a result like in the key-section this: ["verband/regional/landes/kreis"] leaving out the last part of the document-id: "id": "verband/regional/landes/kreis/staffel"

What I need is a prefix result like this: "verband/regional/landes/kreis"

Any kind of hint highly appreciated, I got stuck (again)

SMut commented 13 years ago

PHEW, got the solution. If couchDB gets the idea to emit a single string as an array() you need to push the key you want to search for as an array too. I used: $keys=array("string/to/search/for"); $result = $client->_keys_($keys)->getView('show','like'); I thought keys is the right function to use, but it's not :-/ $key=array("string/to/search/for"); $result = $client->_key_($key)->getView('show','like'); Works instead like a charm and exactly how I need it.

So, what situation is needed to use 'keys' ?

Kind regards, Steffen (happy)