mirror of
https://github.com/doctrine/orm.git
synced 2026-03-23 22:42:18 +01:00
Passing a string to \DateTime results in opaque format() error #5486
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @smcjones on GitHub (Mar 29, 2017).
Originally assigned to: @Ocramius on GitHub.
(Sorry, hit enter accidentally on last issue, leaving it empty)
The issue
When you create a \DateTime entity that is not nullable, and assign a default, Doctrine fails. This appears to be because
\Doctrine\DBAL\Types\convertToDatabaseValuefails to do the following:Example
A fix
It appears the solution might be to change this line:
to this:
or this:
Edits Made
This was pretty poorly thought out at first. I would still like to see Exception handling.
This is my first attempt to contribute to Doctrine2 in any way, so I am reluctant to make a PR and do it incorrectly.
@Ocramius commented on GitHub (Mar 29, 2017):
You wrote:
This is invalid, as a
DateTimeinstance should be provided.The correct approach would be:
An alternative approach is to also pass in a
DateTimeinstance:The DBAL type does not work with strings, closing as
invalid.@smcjones commented on GitHub (Mar 29, 2017):
Interesting. Could be Symfony-related, but a reverse migration for a legacy app created a string default. Not entirely sure why the PHP mapping can only go one way. It feels a bit arbitrary and unhelpful, especially in annotation format. Thanks anyway.
@Ocramius commented on GitHub (Mar 29, 2017):
This particular type only understands
DateTimeandnull. Other types understand other type conversions only.It is arbitrary:
So far, a crash due to an invalid
format()call on a non-DateTimeobject is the best solution.@smcjones commented on GitHub (Mar 29, 2017):
I get it. It sticks to typehinting and it makes things consistent. Passing via constructor is fine and makes a lot more sense than what I was proposing.
As for this issue, it would be nice if you could at least trigger an exception (would that be
Doctrine\DBAL\Types\ConversionException?). This would help tremendously in debugging.@Ocramius commented on GitHub (Mar 29, 2017):
Possibly, but the code you are looking at is extremely performance
sensitive. In php 7, you just get a throwable...
On 29 Mar 2017 2:44 p.m., "Sean" notifications@github.com wrote:
@smcjones commented on GitHub (Mar 29, 2017):
Fair point. I plan on upgrading as soon as I can - as I mentioned, I'm migrating a legacy app. Ultimately adding compatibility for a version of PHP that people are moving away from is probably not the right move...
This will be my last comment: You may want to note in Requirements under Architecture that while it works on PHP 5.4, it is highly recommended that you use PHP 7+.
There are good reasons not to include Exceptions to shave off some milliseconds in software like this, but it makes it significantly harder to track down bad code in PHP 5.6 and below. Unfortunately, that's where most legacy app conversions start out.
@Ocramius commented on GitHub (Mar 29, 2017):
@smcjones yeah, but doctrine operates (at runtime) under the assumption that entities stay valid at all times. Indeed, having a nicer exception would be cool, but not if it impacts the actual throughput (it will in this case).