-
Notifications
You must be signed in to change notification settings - Fork 280
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
Add support for DateTime() #891
Comments
Can you give an example of a more complex situation involving intervals? |
Sure! Let's say that we want to calculate the user's trial end date: $now = new DateTime();
$user = R::dispense('user');
$user->registered = $now;
$user->trialEnd = $now->add(new DateInterval('P7D'));
R::store($user); Considering the above, if RedBean had support for |
Okay, so all you want is for de setter to recognize this object? But what about loading? You don't want RedBeanPHP to turn every text that looks like a date into a DateTimeObject... Or is it not that useful for loading? |
Well, adding support for the setter would be incredibly useful too, but that would represent a significant breaking change unless it was implemented using a flag or similar. |
I think modifying the setter can be implemented in a backward compatible manner. Modifying the getter requires a little thought. Maybe something like $date = $user->box('DateTime')->trialEnd or $user->getDateTime()->trialEnd ? |
Yes, I think modifying the setter shouldn't cause any issues with maintaining backwards compatibility. Regarding the getter, I currently implement this using // Is it a valid datetime?
if (is_string($value) && \DateTime::createFromFormat('Y-m-d H:i:s', $value) !== false) {
return \DateTime::createFromFormat('Y-m-d H:i:s', $value);
}
// Is it a valid date?
if (is_string($value) && \DateTime::createFromFormat('Y-m-d', $value) !== false) {
return \DateTime::createFromFormat('Y-m-d', $value);
} I think the latter syntax you've suggested for the getter is probably cleaner and more obvious to newer RedBean users, but I wonder if it may be better to use something like |
Sounds good, I do some research and try some solutions |
Thanks! I am happy to assist in any way possible. |
Okay that's great. I have to admit that I didn't try it, as I was just using an abstract model to do it. Adding support for getters would be great though. |
Would this be a solution? |
I think that's a perfect solution! |
Thanks for your work on this @gabordemooij , I think this is a great addition to the library. I really like the solution for fetching a bean's value and casting it to an object of a specific type too, that's really neat. |
Sorry to re-open this, but I wonder if the type check referenced here ( redbean/RedBeanPHP/OODBBean.php Line 1262 in a15d6da
Just so that we have support for additional PHP Date instances, such as |
While I agree with you, DateTimeInterface seems to have been added in 5.5. |
Hmm, is there a particular reason for the support of 5.3? Given that security patch support ended 8 years ago, surely it must be considered EOL by now? |
There's one reason called Gabor :D |
@benmajor Personally when i need functionality like this use custom getters and setters in the model public function setLogintime(CarbonImmutable $logintime){
$this->bean->logintime = $logintime->format(MYSQL_DATETIME_FORMAT);
}
public function getLogintime(): CarbonImmutable {
return CarbonImmutable::createFromFormat(MYSQL_DATETIME_FORMAT, $this->bean->logintime);
} |
@marios88 yes, this functionality can be implemented via custom getters and setters. Indeed, that's how I've implemented such functionality to date. But given that there's a core test for |
I will look into this. |
@Lynesth @marios88 @benmajor maybe I'm wrong but it seems that instanceof just does not care if the actual class exist, so we can just change this without sacrificing any backward compatibility I think... I know my obsession with backward compatibility is a bit over the top, but on the other hand, if we can fix it without breaking bc, that's always nice. The internal RedBeanPHP code may be a bit "hairy" because of it but it's not like you have to work inside the library code all the time. |
Add check DateTimeInterface, Closes #891.
This is more a topic of discussion than a bug, but I thought it may be worth mentioning. It would be super-handy if RedBean was aware of
DateTime
assignments to bean properties. I know we can achieve the same functionality by usingformat()
, or implement it using models, but the following would be extremely convenient:Of course in the above instance, we could easily switch it out for
R::isoDateTime()
, but if we're doing some sort of complex stuff with intervals, the above would be a nice feature IMO.The text was updated successfully, but these errors were encountered: