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: Model insert method always insert with current datetime on updatedField #3469

Closed
crustamet opened this issue Aug 9, 2020 · 5 comments
Closed
Labels
bug Verified issues on the current code behavior or pull requests that will fix them

Comments

@crustamet
Copy link
Contributor

Hello i have this issue when i insert a new record always insert with updateField current datetime even if the field is default NULL

I inserted a new record raw from my mysql db to check if mysql automatically inserts this but it is not.

Insert Method from Codeigniter Model has inside these lines

from line 798 - 806

if ($this->useTimestamps && ! empty($this->createdField) && ! array_key_exists($this->createdField, $data))
{
	$data[$this->createdField] = $date;
}

if ($this->useTimestamps && ! empty($this->updatedField) && ! array_key_exists($this->updatedField, $data))
{
	$data[$this->updatedField] = $date;
}

Why the $this->updatedField is even checked here ?

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

I suggest these following lines should be removed from Model public function insert()

if ($this->useTimestamps && ! empty($this->updatedField) && ! array_key_exists($this->updatedField, $data))
{
	$data[$this->updatedField] = $date;
}

@michalsn
Copy link
Member

This is intended. You can extend the Model class and make changes or prepare a PR to make it configurable. Personally I see no issues with current behavior.

@crustamet
Copy link
Contributor Author

I understand, but can i ask why it is intended ?

I just don't understand the reason, why when you create something has modified date set instead of NULL.

Really there is no issue but the thinking is wrong. And because of this all system works the same way.

Think about it when something is created has a modified date on it ?
Why would you make the modified date become the same as created date, just to fill up space for nothing ?

@michalsn
Copy link
Member

Because most of the time it's way easier to work with. There are basically two approaches to this. One is presented by you, and the second one implemented in the Model class. IMO there is no right or wrong solution here. There are trade-offs in both cases.

E.g. if we would like to sort records by updateField we would have to use COALESCE which performs not so great with ORDER and also creates more complicated code. The size of the dataset is not always a primary concern to the developers. The decision not to use null was made to make life easier for developers.

@crustamet
Copy link
Contributor Author

Thanks for reply. I really appreciate your answers.

It seems like if you have updateField to null, you will have to sort the rows by two fields instead of one, still don't know why you would use COALESCE if updateField was null ?

The sort works the same with only ORDER BY.

Anyway, for me it does not make life easier but works fine like this.. i just had to tweak my queries a little.

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

2 participants