parse-community/parse-php-sdk

updating and existing record using this SDk

mzeeshan88 opened this issue · 10 comments

hi dear

can you please help me and give me the example to update an existing record in parse.com using php parse sdk ??
i have tried other solutions like below but no success

$query = new ParseQuery("Event");
$query->equalTo("event_identifier", "31621613216061321630");
$question = $query->find();

// Manipulate the object and save it
$question->set("event_name", "this");
$question->save();

can you help on this?? im really need it and urgent :(

thanks in advanced

kind regards

You don't write details about the issue you have to update a record!

But, I think your problem is that you get an array of records:

$question = $query->find(); // <== here get an array

And can't change the value and update.

Try this:

$questions = $query->find();
$question = $questions[0]; // is better to validate if you get more records and create the code to manage this situation

// Manipulate the object and save it
$question->set("event_name", "this");
$question->save();

The only problem is u r getting array of records with $question =$query->find()
Use $query->first(),it will return only one record and hence u can update successfully

thank you for your reply

i have another problem if i save one record it do work and update the data on parse.com but when i update more than one record it does not update :(

$query = new ParseQuery("Event");
$query->equalTo("event_identifier", $event_identifier);
$question = $query->first();

// Manipulate the object and save it
$question->set("event_name", $event_name);
$question->set("start_date", $start_date);
$question->set("event_identifier", $event_identifier);
$question->set("event_desc", $event_desc);
$question->set("reg_limit", (int)$reg_limit);
$question->set("event_location", $event_location);
$question->set("event_address", $event_address);
$question->set("event_city", $event_city);
$question->set("event_state", $event_state);
$question->set("event_postal", $event_postal);
$question->set("location_list", $location_list);

$question->save();

this code does not update the fields help pleaseeeeee

See ,if u want to update one record then use $question = $query->first();
But if u want to update multiple objects u have to go with $questions = $query->find();
Let's take an example,suppose u want update all events with event name ="event1". Following code will work---->

$query = new ParseQuery("Event");
$query->equalTo("event_name", "event1");
$question = $query->find();
for($i=0;$i;$i++){
$question[$i]->set("event_city", $event_city);
$question[$i]->set("event_state", $event_state)
$question[$i]->save();
}

yes i have fixed the problem thank you very much .. now i have another question i want to delete an record :) what i have to for that?? any idea>>

Easy :)

$question->destroy();  // delete a record

or

$question->delete("fieldname");  // empty a field on a record

You use destroy,first find using Id or any other parameter,nd use destroy same as u used save(),first() for single record and find() with loop for multiple

https://parse.com/docs/php/guide to know more about Parse.com PHP

thank you very much soniurvashi11 and luisbatista really appritiate your help and support :)

Anytime :)