dready92 / PHP-on-Couch

Data Access Library to access a CouchDB server with PHP.

Home Page:http://dready.byethost31.com/index.php/display/view/192

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Creating a doc with pre-defined array

opened this issue · comments

I know you can create a doc like so:

$new_doc = new stdClass();
$new_doc->title = "Some content";

And then one by one, add new items to the array like we see above with title. But if I already have an array that I want to add to the doc stored into let's say $doc_array, how would I do that?

It seems unclear to me. Just to be sure, you're trying to add predefined key-value pairs stored into an array to an existing document of type object?

Example :

<?php

$doc = $client->getDoc('existingId');
$newKeyValue = array('prop1'=>'value1','prop2'=>'value2');

//Merge existing values

$docAsArray = (array)$doc; //Make it resursive it you have multiple levels
$newDoc = array_merge_recursive($docAsArray,$newKeyValue);

@popojargo I want to create a new document, with a predefined array of data already,

I want a document to look like so:

{
    "_id": "whatever-is-generated",
    "name": "My Name",
    "email": "email@email.com"
}

So instead of doing:

$new_doc = new stdClass();
$new_doc->name = "My Name";
$new_doc->email = "email@email.com"

I want to do something like:

$new_array = array("name" => "My name", "email" => "email@email.com");

Simply do this

$new_doc = (object)array("name" => "My name", "email" => "email@email.com");

@bgold0 You can close this issue if it's resolved since I'm not an admin on this repository. If you have any request, feel free to create an issue on my fork since this repo si mostly inactive.

Thank you @popojargo this is exactly what I was looking for.

PS, @popojargo - Thank you for forking this repo and keeping it up to date. We (or I) really appreciate it.