CodeIgniter Forums
Update aka 'touch' database - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Update aka 'touch' database (/showthread.php?tid=77323)



Update aka 'touch' database - bogus - 08-16-2020

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?


RE: Update aka 'touch' database - InsiteFX - 08-17-2020

$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.


RE: Update aka 'touch' database - bogus - 08-17-2020

(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?


RE: Update aka 'touch' database - Gary - 08-18-2020

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.


RE: Update aka 'touch' database - InsiteFX - 08-18-2020

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); 



RE: Update aka 'touch' database - Gary - 08-18-2020

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

Thanks InsiteFX.