mongoDB, PHP update specific value not all the values - php

I am having a problem in updating values i get from web service ..
$collection = $modb->$table;
$collection->update(array("id" => (int)$row['id']),
array('$set' => array(
"user_id" => (int)$post_data_array['user_id'],
"story" => (int)$post_data_array['story'],
"surprize_sub1" => (int)$post_data_array['surprize_sub1'],
"surprize_sub2" => (int)$post_data_array['surprize_sub2'],
"surprize_sub3" => (int)$post_data_array['surprize_sub3'],
"exr_solve" => (int)$post_data_array['exr_solve'],
"exr_assessmnt" => (int)$post_data_array['exr_assessmnt'],
"exr_refresh" => (int)$post_data_array['exr_refresh'],
"sound_control" => (int)$post_data_array['sound_control'],
"clock_control" => (int)$post_data_array['clock_control'],
"switch_user" => (int)$post_data_array['switch_user'],
"exr_print" => (int)$post_data_array['exr_print'],
"write_on_wall" => (int)$post_data_array['write_on_wall'],
"switch_letter" => (int)$post_data_array['switch_letter'],
"view_controls" => (int)$post_data_array['view_controls'],
)));
I get these values from end users.. i want the specific field sent to be updated without loosing all the rest of data ..
in this code only sent data is set while removing the rest .. i want to change only sent ones by keeping the rest as they are, please advice

you need to use updateOne instead of update .
updateOne
Use the MongoDB\Collection::updateOne() method to update a single document matching a filter.
$collection = $modb->$table;
$collection->updateOne(array("id" => (int)$row['id']),
array('$set' => array(
// .... array elements
)));

Related

How to update many fields at once in mongoDB PHP? [duplicate]

The following code should work. I could have missed something, but right now I have it as 2 separate update statements and have decided to ask here why this line isn't working.
$this->db->settings->update(array('_id' => $mongoID),
array(
'$set' => array('about' => $about),
'$set' => array('avatar' => $avatar)
)
);
Did I miss something when reading guides or is it only possible to do with separate update statements?
The third argument to MongoCollection::update is an array of options for the update operation.
$this->db->settings->update(
array('_id' => $mongoID),
array('$set' => array('about' => $about, 'avatar' => $avatar))
);

How do I loop through an object and display its data within an associative array

First of all this is my code:
Log::create([
'action' => $event->changes[0], //<- This is my problem
'object_id' => $event->id,
'object_type' => "Account",
'ip_address' => Request::ip(),
'user' => ucfirst(Auth::user()->name),
'time' => Carbon::now()->format('H:i:s'),
'date' => Carbon::now()->format('Y-m-d')
]);
$event->changes is an array which contains many items but i only know how to get 1 specific item at a time using the index [0] or [1] etc.
How do I get all the values to display instead of 1 at a time? Obviously I don't want to create a new log for each single action but I cant figure out how to do this.
As always any help is appreciated thank you.
You need to serialize the $event->changes if you want it within one log entry.
It actually depends on the structure of changes array, but it seems to be an array of strings, so you may for example use the implode(', ', $changes), so the snippet would look the following:
Log::create([
'action' => implode(', ', $event->changes), //<- This is my problem
'object_id' => $event->id,
'object_type' => "Account",
'ip_address' => Request::ip(),
'user' => ucfirst(Auth::user()->name),
'time' => Carbon::now()->format('H:i:s'),
'date' => Carbon::now()->format('Y-m-d')
]);

Set multiple fields with one update query

The following code should work. I could have missed something, but right now I have it as 2 separate update statements and have decided to ask here why this line isn't working.
$this->db->settings->update(array('_id' => $mongoID),
array(
'$set' => array('about' => $about),
'$set' => array('avatar' => $avatar)
)
);
Did I miss something when reading guides or is it only possible to do with separate update statements?
The third argument to MongoCollection::update is an array of options for the update operation.
$this->db->settings->update(
array('_id' => $mongoID),
array('$set' => array('about' => $about, 'avatar' => $avatar))
);

Update multiple rows using PHP SQLBuilder

I need to edit some PHP that wasn't written by me, I've gotten most of it done but there's a bit I can't figure out the correct syntax for.
Basically we grab data from a Google Fusion Table and populate a table with it, once we've edited the data in the table we can update that information to a production version of the same table. One of the update queries looks like this:
if($table_row[12]=="New"){
$tableid1 = '1bsSleKDBdkhQZfn-oADx0tUtoOc32RqIyiX05Bo';
$insertresults = $ftclient->query(SQLBuilder::insert($tableid1,
array('SUBURB'=> $table_row[1],
'ROAD_NAME' => $table_row[2],
'DESCRIPTION' => $table_row[3],
'DIRECTION' => $table_row[4],
'STATUS' => $table_row[5],
'SITE_ID' => $table_row[6],
'COMMON_NAME' => $table_row[7],
'Lat' => $table_row[8],
'Long' => $table_row[9],
'OPERATING_DAY' => $table_row[10],
'OPERATING_HOURS' => $table_row[11],
'Version' => "current")));
$insertresults = explode("\n", $insertresults);
$rowid1 = $insertresults[1];
$updateresults = $ftclient->query(SQLBuilder::update($tableid,
array('SUBURB'=> $table_row[1],
'ROAD_NAME' => $table_row[2],
'DESCRIPTION' => $table_row[3],
'DIRECTION' => $table_row[4],
'STATUS' => $table_row[5],
'SITE_ID' => $table_row[6],
'COMMON_NAME' => $table_row[7],
'Lat' => $table_row[8],
'Long' => $table_row[9],
'OPERATING_DAY' => $table_row[10],
'OPERATING_HOURS' => $table_row[11],
'Version' => "current",
'Edited_By' => $table_row[13],
'Date_Edited' => $table_row[14]),$table_row[0]));
$updateresults = explode("\n", $updateresults);
$rowid2 = $updateresults[1];
}
What I need to do is write a similar type of SQLBuilder query that will update every record with the same SITE_ID when SUBURB is changed in one of them (i.e., if a record has SUBURB Sydney, SITE_ID 1, each record with SITE_ID 1 must be changed if one record's SUBURB value is changed from Sydney to Melbourne). Not sure exactly how to phrase this syntactically or how I would go about writing the query using SQLBuilder. Any helpw would be appreciated!
SQLBuilder is obviously a locally included class. You will need to look at its documentation or the update method itself in order to see how it expects a where clause to be coded.
The SQL for this would be:
"UPDATE tableid SET SUBURB = '{$table_row[1]}' WHERE SITE_ID = '{$table_row[6]}'"
If you are able to run pure SQL then that will do it. Otherwise I'd need to see the Class libraries.

Dynamically add fields to http_build_query

I have built an API that I want to test. By that reason I'm building a simple client to try out the different features (CRUD). Below is the function for updating a producer, which works fine. However, I also want to be able to update parts of a producer, e.g. address (/producers/8?method=put&address=milkyway).
The array producer always contains the same elements (name, address, zipcode etc) but I only want to update the producer with the elements in the array which contains of anything. What I mean with that is that if for example the name element in the array is empty then name shouldn't be included in *http_build_query*. If only the name element contains of anything then only name should be updated.
So, let's say that the array (except for id that of course is mandatory) contains of address. How can I dynamically add only that to *http_build_query* ?
Thanks in advance!
public function UpdateProducer($producer) {
$url = 'http://localhost/webbteknik2/Labb2/api/v1/producers/ . $producer['id'] . '?method=put';
$data = http_build_query(array(
'name' => $producer['name'],
'address' => $producer['address'],
'zipcode' => $producer['zipcode'],
'town' => $producer['town'],
'url' => $producer['url'],
'imgurl' => $producer['imgurl'],
'latitude' => $producer['latitude'],
'longitude' => $producer['longitude'],
));
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
...
the rest of the curl code
}
Note: I know this is bad coding in many ways, but as I said I only, asap want to be able to test the CRUD functionality through the client.
use array_filter to remove the empty elements....
$params = array(
'name' => $producer['name'],
'address' => $producer['address'],
'zipcode' => $producer['zipcode'],
'town' => $producer['town'],
'url' => $producer['url'],
'imgurl' => $producer['imgurl'],
'latitude' => $producer['latitude'],
'longitude' => $producer['longitude'],
);
$data = http_build_query(array_filter($params, 'is_null'));

Categories