$this->Agreements->save() - create only one record, why? - php

I need to read all records from table agreements, make changes in filed payments, and update all records, save to table. So, my problem is, that save() only create empty record. Do not update exists record. I show you how:
Reading from table:
$agreements = $this->Agreement->find('all');
$payments = $this->Payment->find('all');
Manipulation on fields (part of)(example):
$id=0;
foreach ($agreements as $agreement):
for ($i=$first_agreement; $i<=$last_agreement; $i++){
if ( $agreement['Agreement']['agreement_number']==$i){
$agreements[$id]['Agreement']['payment']=$payd[$i];
}
}
$id++;
endforeach;
Writting to table:
$this->Agreement->save();
A echo debug($agreements) shows correct array, i have tryed also :
$this->Agreement->save($agreements);
or
$this->Agreement->save($this->request->data);
Can you help/explain me how to write all record?
Cake 2.5.2 PHP : 5.4.4-14

Model::save() only saves a single record. If you want to save multiple records you need to use Model::saveAll() or Model::saveMany().

Related

Laravel Crud Application Editing a record

I have a problem with my Laravel crud application for Registrations.
There are these tables: Registration, ExtraRegistration (with a registration_id, extra_id and extraoptions_id),
Extra and ExtraOptions (with the Extra_id).
In the RegistrationController when i add a Registration it makes a new record in the ExtraRegistration with the extraoptions_id and the extra_id. the extra_id is the name of the option and the extraoptions_id is the id of the option you selected.
But now, when you click on edit a record, it shows all the information. the problem is that when you change the extraoption, it makes another record, and not change the select.
And when you have edited something and you look at it again, it still shows the option before you edited it.
RegistrationController
$options = Extra::where("exa_form_id", $distance->asd_form_id)->get();
foreach($options as $option){
$input_name = "option_" . $option->exa_id;
$input_option = $request->$input_name;
if(!is_null($input_option)){
$input_name_extra = "extraoptions_" . $option->exa_id;
$input_option_extra = $request->$input_name_extra;
$registrationextra = new ExtraRegistration();
$registrationextra->iea_registration_id = $registration->isg_id;
$registrationextra->iea_extra_id = $input_option;
$registrationextra->iea_extraoption_id = $input_option_extra;
$registrationextra->iea_price = $option->exa_price;
$registrationextra->save();
}
}
$registration->isg_options = $input_option;
$registration->isg_option_extra_id = $input_option_extra;
Edit:
I want a check before it makes a new ExtraRegistration. that if the price that is in the registration already exists in that registration_id(in ExtraRegistration) it doesnt make another record in the ExtraRegistration.
I want to use an if to fix the problem, but not sure where to begin..
Thanks in advance!
It will probably be easier for you if you just use firstOrCreate method that Laravel provides out of the box.
You can check it here:
https://laravel.com/docs/5.8/eloquent#other-creation-methods
That way, you will be able to retrieve the record that you are looking for, if there is any and then perform all the changes on it. If that record doesn't exist, Laravel will create the new record for you which you'll be able to update.
I hope this helps!
You are creating new object of ExtraRegistration() so its inserting/creating new record every time.
Just changes this line
$registrationextra = ExtraRegistration::find($id);//any id to fetch record

Laravel Query builder Delete Item Notification

I am trying to delete a entry from a table named project in Laravel 4.2 query based on a input data.
What I have done is -
DB::table('project')->where('project.id', '=', Input::get('id'))->delete();
But the problem is, how can I know that if the data is deleted or not?
Can anypne please help?
$id = Input::get('id');
$entryObj = ProjectModel::findorfail($id); // Assuming you have model
// It's bad practise to delete on DB:table because you won't be able to use
//soft-deletes or observers won't trigger if you don't use models.
if($entryObj){
if(!$entryObj->delete()){
throw new Exception('Now we know that it failed to delete');
}else{
echo "100% record is deleted.";
}
}
But let's say you don't care for models/observers/soft-deletes in that case
if(DB::table('project')->where('project.id', '=', Input::get('id'))->delete()){
echo "successfully deleted";
}
Mind you it's very very very bad practise first we not testing what Input::get('id') going to have it could be elephant or a cat in there or let's say it was supposed to have userId=1 but since they wanted to mess your data they modified on client side html to say it's 50. Boom there goes data of user 50 which is disaster if you think about it.
Forgot to mention shortcut
If it is id then you can also use
ProjectModel::destroy($id); //to delete
The delete statement returns a number of how many records where affected. Hence you can check if the return value is greater than zero. In your case it should be always 1 assuming the id is unique. So an if statement if equals one then delete was successfull

How to update a current row on Parse.com using php

I want to update single row base on id, i have questions table and there is column question_id, which has unique date for each row.
Now i want to update that row column based on that question_id.
I have code written below, but not getting success. It created New Record not update old record. I want to update old record.
I have tried both ParseObject and ParseQuery but not getting success.
$query = new ParseObject("question");
$query->equalTo("question_id", "2");
$query->set("correct_percentage", "55.5");
$query->save();
You should load the object in question first, manipulate the data and then save it. Your current code, as you already noticed, simply creates new objects each time it is run.
// Fetch the question object where question_id == 2
$query = new ParseQuery("question");
$query->equalTo("question_id", "2");
$question = $query->first();
// .. optionally verify that $question has a value ...
// Manipulate the object and save it
$question->set("correct_percentage", "55.5");
$question->save();

Codeigniter unknown column in where clause?

I am trying to create an update function in Codeigniter using a dropdown menu. Unfortunately, when I click submit I get an error that there is an "Unknown column 'stages.id' in my where clause. I've used the stages.id in other parts of my code to update other fields, and am not sure why this is happening here. Any assistance or thoughts on why this might be happening would be greatly appreciated!
function update_stage_name($id){
$stage_name=array(
'stage_name'=>$this->input->post('stage_name')
);
$this->db->where('stages.id', $id);
$this->db->update('stage_names', $stage_name);
}
This is what the database query looks like according to the error:
UPDATE `stage_names` SET `stage_name` = 'Holly' WHERE `stages`.`id` = '264'
I have several tables, and stages is sort of the main one. Stages each only have one stage_name but a stage_name can belong to multiple stages. Stage_name does not have a foreign key from stages, but stages has a stage_name_id.
EDIT: So it seems the issue was actually that I wanted to update the stages table, and not the stage_names table. But the problem is that the stages table has a stage_name_id but the input from my form is for stage_name. How do I get to the id in the same method? I'm still new to active record and codeigniter and i'm not sure how to proceed.
As the error says you dont have a column that can be referenced using stages.id. Mainly because its the wrong table..
Try..
UPDATE `stage_names` SET `stage_name` = 'Holly' WHERE `stage_names`.`id` = '264'
//Note changed the table name in the where
If you need to use the stages.id and (as you say in your comment) you dont have an FK then you will need to create one as the database isnt smart enough to guess it. So once you have a field in stages_names called stage_id (or similar) you can do..
UPDATE `stage_names` SET `stage_name` = 'Holly' WHERE `stage_id` = '264'
Try this:
function update_stage_name($id) {
$stage_name=array(
'stage_name'=> $this->input->post('stage_name')
);
$this->db->where('stage_names.id', (int)$id);
$this->db->update('stage_names', $stage_name);
}

Add another data coloumn from arduino sketch into mysql database

I found someone do it on wireless from github. I want to insert another column using arduino via ethernet shield to php apache server into mysql database. In the arduino sketch, I add another coloumndata as shown below:
String yourdatacolumn1="yourdata1=";
String yourdata1;
int yourarduinodata1 = 55555555;
yourdata1 = yourdatacolumn1 + yourarduinodata1;
In the mysql table, I have a column call yourdata1. and in the insert_sql.php. I have modified the code a little bit. But when I run the sketch. On php side, no data is recorded. If I get rid of the new column yourdata1. that works. But when inserting another column , it doesn't work. (I have also created column into mysql called yourdata1)
foreach ($_REQUEST as $key => $value)
{
if ($key == "yourdata") {
$yourdata = $value;
}
if ($key =="yourdata1"){
$yourdata1=$value;
}
}
There are three steps to achieve what you want:
Create a column named yourdata1 in your mysql table: (You have already done as per your question but I am reiterating)I am guessing you followed the steps mentioned in the README of this particular github project to create the columns: yourdata and timestamp. For your problem, you need to create another column, yourdata1 similarly. Or alternatively you can login to the mysql client and issue the following command:
ALTER TABLE your-table-name ADD COLUMN yourdata1 varchar(100)
Edit your insert_mysql.php to start adding data to your new column: Apart from the edit you have shown in the question, you also need to edit the line 23 of the script which inserts data into the table. The syntax for doing so can be found here.I am mentioning it here so that you could use it
INSERT INTO your-table-name (yourdata,yourdata1,timestamp) VALUES ($yourdata,$yourdata1,now());
(Make sure that both $yourdata and $yourdata1 php variables exist, you can initialize both of them as empty, $yourdata='' and $yourdata1='' just before the for loop)
You need to make sure that arduino passes this new yourdata1 to your insert_php script. I guess you have already figured it out. You just need to add another client.println() statement.

Categories