[DDC-657] DateTime objects casted to string in query results Created: 27/Jun/10  Updated: 14/Jan/12  Resolved: 14/Jan/12

Status: Resolved
Project: Doctrine 2 - ORM
Component/s: ORM
Affects Version/s: None
Fix Version/s: 2.3
Security Level: All

Type: New Feature Priority: Major
Reporter: Tomasz Jędrzejewski Assignee: Fabio B. Silva
Resolution: Fixed Votes: 1
Labels: None
Environment:

Linux, PostgreSQL 8.4



 Description   

As we read in the documentation, 'datetime' fields are casted by Doctrine to PHP DateTime class objects. However, when we want to get only certain fields from a table using DQL, Doctrine casts the datetime value to ordinary string. Consider the following example:

Column definition
/**
 * @var DateTime $active
 *
 * @Column(name="registered", type="datetime")
 */
private $registered;
Query
var_dump($em->createQuery('SELECT u.id, u.registered FROM Entities\\User u')->getResult());

Expected result:

  • The returned row contains DateTime object for 'registered' column.

Actual result:

  • The returned row contains string for 'registered' column.

If this is an intended feature, first of all, it should be clearly pointed in the documentation that Doctrine types do not apply in such situations, and secondly, I think it is a very bad practice. I use Doctrine and tell him I have a datetime field in order to get Datetime objects automatically, not to deal with manual conversions.



 Comments   
Comment by Benjamin Eberlei [ 27/Jun/10 ]

oh wait, i am just now looking at your query, you are retrieving the values as scalar fields, that is something totally different. Let me check the code on this issue.

Comment by Tomasz Jędrzejewski [ 27/Jun/10 ]

ORM - the newest one from Git. I couldn't find any matching version in the issue form, so I selected nothing.
DBAL - the version that currently works with ORM. I've already noticed that DBAL-master does not work with the newest ORM .

Comment by Benjamin Eberlei [ 27/Jun/10 ]

Ok the problem is that currently scalar values retrieved from a DQL query are not converted to their PHP value. This affects scalar values only, if you rewrite the query to:

SELECT u FROM Entities\User u

you will get the Entities\User class with a DateTime instance in the "registered" variable.

Comment by Tomasz Jędrzejewski [ 28/Jun/10 ]

So far, I simply added a manual conversion that checks if the object was returned or not, so it will still work once the bug is fixed. In my case, sometimes I prefer to retrieve certain columns only, because the entire row can be quite big.

Comment by Benjamin Eberlei [ 01/Jul/10 ]

I think we should really fix this using an additional bit of information on the ResultSetMapping to hint the hydrators which type a scalar value is, if its an entity field, otherwise do nothing in conversion.

Array or Object fields, or even complex objects and serialization structs are pretty useless otherewise

Comment by Roman S. Borschel [ 07/Aug/10 ]

Scheduled for 2.1 for now.

Comment by Benjamin Eberlei [ 15/Sep/10 ]

Convert to feature (from bug)

Comment by Jan Volf [ 24/May/11 ]

I'm observing this problem on ORM 2.0.3 package with MySQL database and I would really expect this conversion to be implicit.
The Docs (12.7.1. Query Result Formats) says:

Query#getArrayResult(): Retrieves an array graph (a nested array) that is largely interchangeable with the object graph generated by Query#getResult() for read-only purposes.

But it is not interchangeable at all.

Anyway: It can potentially be an issue when dealing with timezones as the as it exposes the date in it's original format stored in DB, eventually without timezone on most DB engines. To interpret the such date the same way as Doctrine when hydrating to Object, one would need to know what timezone uses doctrine to interpret the DB timestamps without timezone.

Comment by Guilherme Blanco [ 13/Jan/12 ]

We have a nearly done PR for this issue: https://github.com/doctrine/doctrine2/pull/251
As soon as Fabio changes the pointed optimizations, this issue will be closed.

Comment by Fabio B. Silva [ 14/Jan/12 ]

Fixed since this commit : https://github.com/doctrine/doctrine2/commit/ea14bcff4a2a78bf774e8847b6645dca112f9757

Generated at Sun Apr 20 18:48:13 UTC 2014 using JIRA 6.2.3#6260-sha1:63ef1d6dac3f4f4d7db4c1effd405ba38ccdc558.