Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: set() doesn't work on DateTime database field types #3471

Closed
k-msalehi opened this issue Aug 9, 2020 · 3 comments
Closed

Bug: set() doesn't work on DateTime database field types #3471

k-msalehi opened this issue Aug 9, 2020 · 3 comments
Labels
bug Verified issues on the current code behavior or pull requests that will fix them

Comments

@k-msalehi
Copy link
Contributor

$loginTime=date('Y-m-d H:i:s')
$userModel->set(['lastLogin' => $loginTime])->where('id',$user['id'])->update();

it should Update the lastLogin field with $loginTime value.
but it produces an error: You must use the "set" method to update an entry.
when I change 'lastLogin' to a text field type it works correctly.

CI v4.0.4
OS: Windows 10
Web server apache
PHP v7.4

@k-msalehi k-msalehi added the bug Verified issues on the current code behavior or pull requests that will fix them label Aug 9, 2020
@InsiteFX
Copy link
Contributor

InsiteFX commented Aug 10, 2020

You know you can have MySQLi automatically update the datetime fields if you setup your tables like this:

'created_at' DATETIME DEFAULT CURRENT_TIMESTAMP,
'updated_at' DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
'deleted_at' DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,

Then you do not have to worry about them being set. Make sure that you escape the field names had to change to single quotes for it to insert into the code tags.

@paulbalandan
Copy link
Member

I think this error is due to method overloading. Not sure if this will work but can you try this:

$userModel->where('id',$user['id'])->set(['lastLogin' => $loginTime])->update();

@michalsn
Copy link
Member

@InsiteFX Using CURRENT_TIMESTAMP is not really applicable here... also this method has two pitfalls - it does not always use the timezone we want (and we're not always able to set it to what we need). And the second is that the value is always updating ;)

@pars0097 Please check if you have lastLogin listed in your model's $allowedFields.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Verified issues on the current code behavior or pull requests that will fix them
Projects
None yet
Development

No branches or pull requests

4 participants