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

        Tomasz Jędrzejewski created issue -
        Tomasz Jędrzejewski made changes -
        Field Original Value New Value
        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.
        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:

        {code:title=Column definition|borderStyle=solid}
        /**
         * @var DateTime $active
         *
         * @Column(name="registered", type="datetime")
         */
        private $registered;
        {code}

        {code:title=Query|borderStyle=solid}
        var_dump($em->createQuery('SELECT u.id, u.registered FROM Entities\\User u')->getResult());
        {code}

        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.
        Benjamin Eberlei made changes -
        Comment [ Can you specify exactly which versions of Doctrine 2 you are using? i.e. what ORM package and what DBAL package? We made a pretty significant change in the conversion in the last days, it maybe that you mixed the wrong packages. ]
        Roman S. Borschel made changes -
        Fix Version/s 2.1 [ 10022 ]
        Benjamin Eberlei made changes -
        Issue Type Bug [ 1 ] New Feature [ 2 ]
        Benjamin Eberlei made changes -
        Fix Version/s 2.x [ 10090 ]
        Fix Version/s 2.1 [ 10022 ]
        Fabio B. Silva made changes -
        Assignee Roman S. Borschel [ romanb ] Fabio B. Silva [ fabio.bat.silva ]
        Fabio B. Silva made changes -
        Status Open [ 1 ] Resolved [ 5 ]
        Fix Version/s 2.3 [ 10185 ]
        Fix Version/s 2.x [ 10090 ]
        Resolution Fixed [ 1 ]
        Benjamin Eberlei made changes -
        Workflow jira [ 11558 ] jira-feedback [ 14477 ]
        Benjamin Eberlei made changes -
        Workflow jira-feedback [ 14477 ] jira-feedback2 [ 16341 ]
        Benjamin Eberlei made changes -
        Workflow jira-feedback2 [ 16341 ] jira-feedback3 [ 18594 ]

          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: