Welcome Guest, Not a member yet? Register   Sign In
Update aka 'touch' database
#1

(This post was last modified: 08-16-2020, 10:40 PM by bogus.)

Day

I like to 'touch' a database row.
Means, I want to update only the updated_at field, how can I do that in my controller with the instance of my model?
Reply
#2

$useTimestamps

This boolean value determines whether the current date is automatically added to all inserts and updates.
If true, will set the current time in the format specified by $dateFormat. This requires that the table
have columns named ‘created_at’ and ‘updated_at’ in the appropriate data type.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

(This post was last modified: 08-17-2020, 10:53 AM by bogus.)

(08-17-2020, 03:21 AM)InsiteFX Wrote: $useTimestamps

This boolean value determines whether the current date is automatically added to all inserts and updates.
If true, will set the current time in the format specified by $dateFormat. This requires that the table
have columns named ‘created_at’ and ‘updated_at’ in the appropriate data type.

Ok, thanks, understood.

Your reply implies now that I need to use the base-class method 'update' to update a row or a field/fields in the table, correct?

The whole point I'm posting this is, that I only need to update the column updated_at and no other.
I understood the docs and your resembling reply that both fields are updated with a timestamp of my chosen format, correct?

This means unwanted behavior.

Leaving created_at empty in my model isn't going to be a solution nor to work because it will throw an exception.

Further, how can I update a row without changing it's content?

As I have understood the docs, you need to change a field to be able to update and update the updated_at and created_at fields with a timestamp of format x.


Do you have some code suggestions?
Reply
#4

I'm having the same problem... and with the updated_at field being managed automatically by CI, I'm reluctant to write to it myself (could it cause a conflict/locking contention under certain circumstances, or perhaps two writes?).

Does one HAVE to re-write something else in the row to trigger this, or is there a better way?

Thanks.
Reply
#5

I looked at the Model and what I found is that Inserts and Updates update the created_at and updated_at fields.

But the Model replace does not updated the created_at and updated_at fields.

So you should be able to change them using:

PHP Code:
$now date("Y-m-d H:i:s");

$data = [
    'updated_at' => $now,
];

$builder->replace($data); 
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#6

Great! That looks like a simple and tidy work-around.

Thanks InsiteFX.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB