In one of my controllers, I am needing to update a specific record in another table. I have code to find the table, update the specific column, and save it. My problem is that the record is never updated. I have ensured that the column is fillable.
Here is the code that is giving me problems:
$portfolio = Portfolios::where('id', $request->portfolioID)->first();
$portfolio->totalValue = $portfolio->totalValue + $cd->currValue;
//dd($portfolio);
$portfolio->save();
At the die and dump that is commented out, I can compare the attributes shows the correct value and the original value was the previous value, which is still retained after the save.
Related
I have a number of select fields in which I'm saving the data to an associated table that stores two ids (no id column on the table, just two id's from each associated table). I have no problem with actually creating a record and after some tinkering updating an existing record. select example
What I'm having trouble with is using Laravel updateOrCreate method, it doesn't update the original record but creates a new one + the old one. Basically, creates a second record instead of updating the original.
I'm not entirely sure what to do here. If I've already set a select value in the database but want to update that particular one, as well as have the option to set the second select how would I go about doing it? Code example below of how I'm getting my current (incorrect) solution
$contractors = $request->get('contractors');
if($contractors) {
foreach ($contractors as $contractorId) {
if (isset($contractorId)) {
$interestedContractor->updateOrCreate([
'project_id' => $project->getId(),
'contractor_id' => $contractorId
]);
}
}
}
As this is my first question if anything is unclear please let me know and I will clarify.
Also the relationship is setup by a hasManyThrough if that helps at all.
I've been struggling with this problem for a while now and can't figure out reasonable explanation for this behaviour...
I have in MySQL a table called "notifications" and columns "package_id", "mail_sent_at" and "sms_sent_at", and appropriate Propel model for that.
I'm setting "sms_sent_at" column with current time and saving it. Works perfectly.
$not = new Notification();
$not->setPackageId($package_id);
$not->setSmsSendedAt(time())->save();
The result for this is:
Now I want to add one more thing - set mail_sent_at column. So what I do:
$not = new Notification();
$not->setPackageId($package_id1);
$not->setMailSendedAt(time())->save();
$not->setSmsSendedAt(time())->save();
And here's the magic: only mail_sent_at column was set.
Additional notes:
after var_dump'ing i can see that dumped object has both properties set after executing these operations
var_dump'ing object retrieved from propel query by package_id after these operations executed doesn't have the second column set. As it is in DB.
Same behaviour happens other way around if I first set different column - first saved column goes into DB, second don't.
Why is that happening and how can I fix it?
Ps. Yeah, I know it's "sent", not "sended"
EDIT
Finally I found the solution. In case someone is having similar problem:
check if your primary key in propel schema has autoIncrement="true". If not - add it!
What was happening was: I've created a new object with id=null, saved it to DB using implicitly SQL INSERT (since it's new record ) and while working on the same object modified it and saved it to DB once again, but this time imlpicitly by SQL UPDATE (Propel figures out that I inserted this object once). The problem was, that after first saving I didn't get id for my object set (even though there was ID set in DB). So second SQL contained "UPDATE...WHERE id = NULL".
I have a web page with a grid of row and columns and I need to keep track of activity that happens in a given cell. Items can be moved around between cells so I keep a record in a MySQL table to track the contents of each cell. The key to the db record is row number and column number, for example, the key to the record representing row 2, column 7 is r2c7.
When an item gets put into r2c7, I insert a record with r2c7 as the key field value. If the contents of r2c7 gets move to r3c8, I insert a record into r3c8 and delete the record for r2c7. All is fine up to this point.
What I am finding though, is that after I've deleted a record for a cell, say r2c7, if I then try to re-insert the same record back in for r2c7, the insert does not stick, even though MySQL reports success on the insert. To be clear, all fields in the record that I previously deleted, are the same when I go to re-insert. It seems the database sees a duplicate record trying to be re-inserted and does not process it, even though success is reported.
Anyone have an idea of what is going on?
Check mysqli_affected_rows($con) ;
It will definitely gives you a false as return if the row insert was unsuccessfull
I am building a system, where you can create blog posts for your website.
In there I have an ajax-function which is saving a draft of your post every two minutes.
In this way you're not losing your work if your computer or internet crashes.
But right now, it is saving a new row every time it auto-saves. How to I do, so instead of creating multiple rows, it is updating the row instead?
I have already tried with ON DUPLICATE KEY UPDATE, which didn't work. I think it might be because that it requires an unique field in the form. But the only unique in my database is the actual ID of the post/row.
This is the code I tried:
INSERT INTO blog (title, text, date)
VALUES ('$blog_title','$blog_text','".time()."')
ON DUPLICATE KEY UPDATE
title='$blog_title', text='$blog_text', modified_date='".time()."'
I have an idea, to get the post/row ID, when the post is auto-saved the first time. Here I could use mysql_insert_id(). Then this ID could be stored in a hidden input field and when it auto-saves again, it will see that there already is a post/row with that ID, and then it will just update instead of creating a new one.
Is that a good and safe solution, or should I do something else?
I can't seem to find a better one.
I have found some other similar questions, but they where using JSON, which I haven't worked with yet.
Hope someone can help me with this :)
When you create a new row, put the ID into a hidden field. Then the code to process the input can do:
if ($_POST['id']) {
// update existing row
} else {
// insert new row and put ID into hidden field
}
There's no need to use ON DUPLICATE KEY because you know from the input data whether it's indended as a new entry or an update.
I am creating my first (!) database, and I have run into an issue that I cannot seem to locate the answer for.
I have put an "added on" field in a table (among other things ofc), and since I'm the one adding it, I want to put the same date in the entire column. The idea is that if there is a new item added at a later date, it will have that date, but the data initially populated should all have the same date.
How? Please don't tell me one row at a time....
Just add the column to the table and then run an update query
update yourtable set nameofyournewfield = 'yourdate'
This will update all rows currently in the db, while the new rows will gettheir value (or have the default value you provided)
Another possibility in addition to #Nicola's answer is to use the DEFAULT argument in add column.
You can set default property of that column to any date and it will replicate that for all rows as long as you don't specify any value for that column while inserting and when you want to insert a different value just specify it in the insert statement.