Data not saved in database cakephp - php

I have an event table with this fields:
I use this code to insert data in this table :
$oneEvent = array(
'trainer_id' => $event['trainer_id'],
'formation_id' => $event['formation_id'],
'title' => $event['title'],
'start' => $event['start'][$i],
'end' => $event['end'][$i],
);
$success = $this->Event->save($oneEvent);
$events_id= $this->Event->inserted_ids;
when I run this code I get true and ID of insert element (showed using debug)
but in database i can't see this field never !!!!.
and when I insert data in phpmyadmin when this request INSERT INTO events(title,start, end, trainer_id, formation_id) VALUES ('Départ B','2016-11-18 10:00:00','2016-11-18 11:00:00','13','1') it worked
I didn't know what happen here !!??

I solved the problem by using $dataSource->commit(); after calling save() function

Related

How to get the inserted id of create() method in laravel?

I'm trying to get the last inserted id made from create() eloquent in laravel.
Here's my laravel code
$product = ProductModel::create([
'prodCode' => $prodCode,
'prodTitle' => $dataProducts->prodTitle,
'prodDesc' => $dataProducts->prodDesc,
'attachment' => "images/products/".$attachment,
'prodSize' => $dataProducts->prodSize,
'prodCategory' => $dataProducts->prodCategory,
'prodPrice' => $dataProducts->prodPrice,
'created_by' => auth()->user()->id
]);
I will use this last inserted id for another query with the same function.
Is it possible to do it with this way of saving data, or do I need to convert this code to another efficient way?
The create function of a model returns new record.
Very easily:
$product = ProductModel::create([...]);
// last inserted id
$lastInsertedId = $product->$idField;
Well in this case your are doing right !
use print_r($product->id) to see the last inserted id if the field name is id

multiple value insert using saveField function or other function in cakephp

I insert multiple values into my database using saveField function. Here is my code.
$this->Fuel->saveField(
'device_id', $int_fuel_func[0],
'func_code', $int_fuel_func[1],
'device_data', $int_fuel_func[2]
);
It's not a form value. Value automatically insert when the page is refreshed.
It's only inserts first value 'device_id', $int_fuel_func[0],. Other two values are not insert into my database. Any one suggest me how to insert these values.
Thanks in advance
Cake version 2.7.5
You can simply do this as well.
$data = array(
'device_id' => $int_fuel_value[0],
'func_code' => $int_fuel_value[1],
'device_data' => $int_fuel_value[2]
);
$this->Fuel->save($data);
Also try this blog tutorial (cakephp blog tutorial) if you haven't been there before to be more familiar with these things.
My id is not auto Increment it is defined as varchar. Time is not include there also. I got my answer. I use it and it works.
$this->Fuel->set(
array(
'device_id' => $int_fuel_value[0],
'func_code' => $int_fuel_value[1],
'device_data' => $int_fuel_value[2]
)
);
$this->Fuel->save();
saveField function is only for updating one field! use updateAll and pass an array to it :
$this->Fuel->updateAll(
array(
'device_id', $int_fuel_func[0],
'func_code', $int_fuel_func[1],
'device_data', $int_fuel_func[2]
),
array('Fuel.id' => 234) // conditions for the Fuel you want to update
);

Laravel update Issue

Here is my code -
$updatecompany = DB::table('Companies')
->where('ID', (int)$companyid)
->update(array(
'CompanyName' => $companyname,
'CompanyAddress' => $companyaddress,
'CompanyEmail' => $companyemail,
'ContactName' => $contactname,
'CompanyCity' => $companycity,
'CompanyState' => $companystate,
'CompanyZip' => $companyzipcode,
'CompanyPhone' => $companyphone,
));
$updatecompany is always 0. What might be the problem?
One of most possible reasons is that you are updating with the same data in the database.
There needs one out of the box solution, of course if you can do it.
So, no rows are updating, even if the SQL is correct.
Here is my suggestion:
Add a new column updatedOn in DB Table Companies.
The type should be TIMESTAMP and add attribute ON UPDATE CURRENT_TIMESTAMP.
This way you will always get row affected and hence you get return value other than 0.
You don't need to cast $companyId to an integer there. It does not help Laravel's query builder.
Use dd($companyId) and dump the variable before you run the query and find out what it is.

Column in SQL being deleted on UPDATE

I have a table of values that uses a Where clause to find the row and then updates the values in the row. However it is deleting the value it is searching for after it is already found. So it finds the row using the stockid (not a PK) and updates the row, but leaves the stockid blank afterwards
$data = array(
'towSet' => $towSet,
'transWare' => $transWare,
'oceanFreightBooked' => $oceanFreightBooked,
'BOLrec' => $BOLrec,
'BOLsent' => $BOLsent,
);
$this->db
->where('stockid', $stockHold)
->update('logistics_tracking', $data);
The Strange part is that using this code doing the same thing it works perfectly fine
$data = array(
'recTitle' => $recTitle,
'recPOA' => $recPOA,
'recTitleState' => $recTitleState,
'titleSent' => $titleSent,
);
$this->db
->where('stockid', $stockHold)
->update('title_tracking', $data);
So as a reminder all the other values (towSet, transWare, oceanFreightBooked, ect.. ) all insert and it just blanks out the stockid after updating
Any ideas would be greatly appreciated
Yeah the code was fine, apparently I was just doing too much with one function. Created an external function and passed through an array of the data. Weird.
Thanks for looking!

Set field value for single database row

I'm trying to update a row on my profiles table to reset a users profile picture to the default of user.png. I have the following action in my controller:
public function deleteProfilePicture() {
$this->layout = 'ajax';
// First find the profile ID from the user ID
$profileId = $this->Profile->find('first', array(
'condition' => array('User.id' => $this->Auth->user('id')),
'fields' => array('Profile.id'),
'recursive' => -1
));
$this->Profile->id = $profileId['Profile']['id'];
$this->Profile->saveField('picture', 'user.png', false);
}
However, when I request the URL (/profile/deleteProfilePicture) I get no errors but the database row isn't updated. I have made sure the current profile ID is used by using debug($profileId).
What could be going wrong here?
Edit: The return value of saveField():
array(
'Profile' => array(
'id' => '36',
'modified' => '2013-04-05 14:16:57'
)
)
Try
$this->Profile->id = $profileId['Profile']['id'];
$this->Profile->set(array(
'picture' => 'user.png'
));
$this->Post->save();
I see no error in your code. Try seeing what query is getting executed using this
$log = $this->Model->getDataSource()->getLog(false, false);
debug($log);
based on the result make changes to your query if you find something wrong.
Or try using this
$data['Profile']['id']=$profileId['Profile']['id'];
$data['Profile'['picture']='user.png';
$this->Profile->save($data);
If you set debug to 2 you could see what SQL is being executed, see if your update is actually firing.
Try this
$this->Profile->read(null, $profileId['Profile']['id']);
$this->Profile->saveField('picture', 'user.png', false);
Why are you setting the validation to false? Do you get an error if you omit that?

Categories