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: Empty Entity date attributes receive current timestamp when accessed #3251

Closed
JanZelenka opened this issue Jul 8, 2020 · 6 comments
Closed
Labels
bug Verified issues on the current code behavior or pull requests that will fix them

Comments

@JanZelenka
Copy link

I have the following scenario:

  • Using the Entity class
  • Setting the $dates property to an array of attributes that are supposed to be of type DateTime
  • On loading the Entity data from the database, the dates have no value
  • When trying to get the value, __get() doesn't check for an empty value, calls the mutateDate method, which in turn end up calling Time::parse() resulting in the current timestamp.

CodeIgniter 4 version: 4.0.3
Affected module: Entity class
PHP version: 7.2

@JanZelenka JanZelenka added the bug Verified issues on the current code behavior or pull requests that will fix them label Jul 8, 2020
@nyufeng
Copy link
Contributor

nyufeng commented Jul 23, 2020

This is expected, because new DateTime('') return current time. I think this is a problem with your data rather than a problem with the framework.

@JanZelenka
Copy link
Author

Thanks for the feedback but I still believe this is a framework issue. Imagine this scenario:

I have a table Process with column startedOn in my database. When this column is empty, it indicates a process that hasn't started yet.
In CodeIgniter I create an Entity class Process. I load data of a specific process into $CurrentProcess of type Process and try this:
if ( empty( $CurrentProcess->starterdOn ) ) { $CurrentProcess->start() }

My processes never get started because even though in the database they have no startup date specified, CodeIgniter substitutes my empty value with the current timestamp.

Bottom line, if a column is empty in the database, the respective attribute in your Entity must be empty as well.

@nyufeng
Copy link
Contributor

nyufeng commented Jul 24, 2020

I will submit a PR based on your suggestion, but whether to merge or not depends on the judgment of the administrator.

@MGatner
Copy link
Member

MGatner commented Jul 24, 2020

I think the expectation is that you would use null in your database, so the result wouldn't be a string. While I don't know what one would expect from mutating "" I agree getting back the current time stamp is unexpected.

@JanZelenka
Copy link
Author

Yeah, that would indeed be the expectation - except my database engine (FileMaker) doesn't recognize null, "" is its equivalent of null

@michalsn
Copy link
Member

Well... I believe that in the Entity class we have a special use for the empty string and it should not be changed.

@JanZelenka If your database is specific and there is no support for null values then you should probably extend the Entity class and make changes just for your own use.

An empty string should still be supported and return the current date - this is expected behavior.

@MGatner MGatner closed this as completed Jul 28, 2020
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

Successfully merging a pull request may close this issue.

4 participants