Skip to content

Date/Time support is limited #18

Closed
jakzal opened this Issue Jan 19, 2012 · 3 comments

2 participants

@jakzal
jakzal commented Jan 19, 2012

DateTimeType and DateTimeTzType are limited to one date/time format while \DateTime class can easly handle more.

ISO 8601 format (2012-01-11T16:50:15+00:00) is quite common in XML and there's only one way of adding support for it at the moment (creating a new type). It's not the most convenient way. To use a new date format I have to add a new class.

We can easily deal with any PHP supported format:

class DateTimeType
{
    // ... snip

    public function convertToPHPValue($value)
    {
        if ($value === null) {
            return null;
        }

        try {
            return new \DateTime($value);
        } catch (\Exception $exception) {
            throw ConversionException::conversionFailed($value, $this->getName());
        }
    }
}

Converting that value back to an XML might be a bit harder (convertToXMLValue() method). It would be great if we could define it with metadata:

use Doctrine\OXM\Mapping as OXM;

/**
 * @OXM\XmlEntity
 */
class Entry
{
    /**
     * @OXM\XmlElement(type="datetime", format="c", name="updated")
     */
    public $updated = null;
}

format would translate directly to a format supported by PHP's date function.

If it's the right approach I can work on the implementation. Only issue I have is how to access format's value in a type.

@richardfullmer
Doctrine member

I like the concept. Current implementation was modeled from doctrine-dbal's DateTimeType without the AbstractPlatform as this doesn't make a lot of sense for XML serialization.

I'm open to the format concept though. It would be the first time that a type metadata could have parameters, so perhaps we might model more generically and allow parameters to be passed to types in general.

Perhaps something like

type="datetime('c')" 
type="datetime", type_parameters="c" 
type="datetime", type_parameters="{format: c}"

could generalize the concept?

@jakzal
jakzal commented Jan 19, 2012

I guess the last option is the most flexible (type="datetime", type_parameters="{format: c}"). Is Doctrine ORM doing something similar? Might be good to be consistent.

How do you see the implementation?

Marshaller creates types with Type::getType() method. At the moment it creates each type once. We could probably add second (optional) arguments with type parameters but we cannot reuse types anymore (at least those parametrized).

@richardfullmer
Doctrine member

The instance of the type could be stored in the ClassMetadata. I might have some time this weekend to tinker around with it. Or you could send a pull request marked WIP with some ideas..

@jakzal jakzal closed this Jul 21, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.