Doctrine 2 - ORM
  1. Doctrine 2 - ORM
  2. DDC-657

DateTime objects casted to string in query results

    Details

    • Type: New Feature New Feature
    • Status: Resolved
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 2.3
    • Component/s: ORM
    • Security Level: All
    • 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.

        Activity

        Show
        Fabio B. Silva added a comment - Fixed since this commit : https://github.com/doctrine/doctrine2/commit/ea14bcff4a2a78bf774e8847b6645dca112f9757
        Hide
        Guilherme Blanco added a comment -

        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.

        Show
        Guilherme Blanco added a comment - 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.
        Hide
        Jan Volf added a comment -

        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.

        Show
        Jan Volf added a comment - 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.
        Hide
        Benjamin Eberlei added a comment -

        Convert to feature (from bug)

        Show
        Benjamin Eberlei added a comment - Convert to feature (from bug)
        Hide
        Roman S. Borschel added a comment -

        Scheduled for 2.1 for now.

        Show
        Roman S. Borschel added a comment - Scheduled for 2.1 for now.

          People

          • Assignee:
            Fabio B. Silva
            Reporter:
            Tomasz Jędrzejewski
          • Votes:
            1 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: