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

Allow DateTimeImmutable as parameter value #1333

Closed
wants to merge 1 commit into from
Closed

Allow DateTimeImmutable as parameter value #1333

wants to merge 1 commit into from

Conversation

janlanger
Copy link
Contributor

This pull request allows passing instance of DateTimeImmutable as value to setParameter and its correct formating in query.

@doctrinebot
Copy link

Hello,

thank you for creating this pull request. I have automatically opened an issue
on our Jira Bug Tracker for you. See the issue link:

http://www.doctrine-project.org/jira/browse/DDC-3616

We use Jira to track the state of pull requests and the versions they got
included in.

@@ -53,7 +53,7 @@ public static function inferType($value)
return Type::BOOLEAN;
}

if ($value instanceof \DateTime) {
if ($value instanceof \DateTime || $value instanceof \DateTimeImmutable) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about only one instanceof using \DateTimeInterface?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DateTimeInterface isn't defined for 5.4.x

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@stekycz That's available only since PHP 5.5

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know but \DateTimeImmutable too. So when I do not pass instance of \DateTime in PHP 5.4 or older then it will fail on the second part of this condition, right?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Undefined class in instanceof check is not an error, is it?
Well you can still make it like this, if you wanna be pedantic:

if ($value instanceof \DateTime || (PHP_VERSION_ID >= 50500 && $value instanceof \DateTimeInterface)) {

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Majkl578 I like that! 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, undefined class in instanceof is not an error, it returns false. I think complicating the condition is unnecessary, since it's working in proposed form :-) http://3v4l.org/6KEnb

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@janlanger I would still be in favor of checking the interface rather than \DateTimeImmutable to support PHP 5.5+

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean like if ($value instanceof \DateTime || $value instanceof \DateTimeInterface) { ?

Hm, that would be probably better...

@Ocramius
Copy link
Member

Note: test required.

@janlanger
Copy link
Contributor Author

@Ocramius Is the test enough, or do you want something more?

Ocramius added a commit that referenced this pull request Mar 17, 2015
@Ocramius Ocramius closed this in 32137c7 Mar 17, 2015
@Ocramius
Copy link
Member

@janlanger manually merged into master at 32137c7, thanks!

Ocramius added a commit that referenced this pull request Mar 17, 2015
Ocramius added a commit that referenced this pull request Mar 17, 2015
@janlanger janlanger deleted the patch-1 branch March 17, 2015 21:32
greg0ire pushed a commit that referenced this pull request Dec 5, 2020
…e parameters (#8328)

The support for passing \DateTimeImmutable instance as a query parameter has
been added to ORM in #1333 (the year 2015), a long time before immutable date
types (datetime_immutable etc) were introduced to DBAL in doctrine/dbal#2450
(2017).

Back then, it made sense to treat \DateTimeImmutable (or any
\DateTimeInterface) in the same way as \DateTime and infer parameter type as
datetime. However, when immutable date types were later added to DBAL, it
wasn't reflected anyhow in type inference in ORM and \DateTimeImmmutable
instances are still inferred as datetime DBAL type.

This PR fixes this IMO incorrect behaviour of
ParameterTypeInferer::inferType(): for a \DateTimeImmmutable parameter, it now
returns datetime_immutable DBAL type; for \DateTime or any other types
implementing \DateTimeInterface, it returns datetime DBAL type as it did
before.

This behaviour is in line with DateTimeImmutableType handling only
\DateTimeImmutable and DateTimeType handling any \DateTimeInterface.

Why? In most cases, it doesn't matter and datetime works for \DateTimeImmutable
parameters just fine. But it does matter if using custom implementation of
datetime_immutable type like UTCDateTimeImmutableType from
simpod/doctrine-utcdatetime. Then the broken type inference is revealed.

This is partially related to #6443, however, this PR isn't about custom DBAL
types but about correct type inference for build-in types.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants