Doctrine 2 - ORM
  1. Doctrine 2 - ORM
  2. DDC-1209

DateTime (and other custom object types) cannot be used in primary key (singular or composite)

    Details

    • Type: Bug Bug
    • Status: Resolved
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: Git Master
    • Fix Version/s: 2.1
    • Component/s: ORM
    • Security Level: All
    • Labels:
      None

      Description

      This bug was encountered using a DateTime type as an @Id, but it appears that it will affect any type of custom data type if it is an object.

      Doctrine\ORM\Id\AssignedGenerator checks to see if the key is an object. If so it does a look up in the identity map to find the object, but in the case of DateTime it will not find it and throws an exception. It is appears this is the case for both singular and composite keys as shown in the attached test.

      Offending code: https://github.com/doctrine/doctrine2/blob/master/lib/Doctrine/ORM/Id/AssignedGenerator.php#L52

      Test case forthcoming.

        Activity

        Show
        Michael Ridgway added a comment - Test case: https://github.com/mridgway/doctrine2/blob/DDC-1209/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1209Test.php
        Hide
        Michael Ridgway added a comment -

        I posted a fix and it was merged this morning by Guilherme: https://github.com/doctrine/doctrine2/pull/74

        Show
        Michael Ridgway added a comment - I posted a fix and it was merged this morning by Guilherme: https://github.com/doctrine/doctrine2/pull/74
        Hide
        Jonathan Ingram added a comment -

        I am wondering if this fix is working correctly or has regressed?

        I have a YAML file as follows:

         
        Acme\Entity\SearchExecution:
            type: entity
            table: search_execution
            id:
                search:
                    associationKey: true
                # It seems this is not working
                dateExecuted:
                    type: datetime
                    column: Date
            fields:
                executionTime:
                    type: integer
            manyToOne:
                search:
                    targetEntity: Acme\Entity\Search
                    joinColumn:
                        name: SearchID
                        referencedColumnName: id
                user:
                    targetEntity: Acme\Entity\User
                    joinColumn:
                        name: ByUserID
                        referencedColumnName: id
        

        So the "dateExecuted" field is used to compose an artificial primary key since the table has no PK.

        The error is:

        [ErrorException]                                                                                                                                                                                
          Catchable Fatal Error: Object of class DateTime could not be converted to string in vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php line 1295
        
        Exception trace:
         () at vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:1295
         Symfony\Component\HttpKernel\Debug\ErrorHandler->handle() at n/a:n/a
         implode() at vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:1295
         Doctrine\ORM\UnitOfWork->addToIdentityMap() at vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:1125
         Doctrine\ORM\UnitOfWork->scheduleForInsert() at vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:810
         Doctrine\ORM\UnitOfWork->persistNew() at vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:1540
         Doctrine\ORM\UnitOfWork->doPersist() at vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:1501
         Doctrine\ORM\UnitOfWork->persist() at vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php:565
         ...
        

        In my composer.json, the relevant versions of Doctrine are:

            {
                {
                    "package": "doctrine/common",
                    "version": "2.3.0-RC1"
                },
                {
                    "package": "doctrine/dbal",
                    "version": "2.3.x-dev",
                    "source-reference": "239630b61f03f39d198441eced1bfffb7b0e61d1",
                    "commit-date": "1346866589"
                },
                {
                    "package": "doctrine/orm",
                    "version": "2.3.0-RC1"
                },
            }
        

        Is this a bug or am I misusing this?

        Show
        Jonathan Ingram added a comment - I am wondering if this fix is working correctly or has regressed? I have a YAML file as follows: Acme\Entity\SearchExecution: type: entity table: search_execution id: search: associationKey: true # It seems this is not working dateExecuted: type: datetime column: Date fields: executionTime: type: integer manyToOne: search: targetEntity: Acme\Entity\Search joinColumn: name: SearchID referencedColumnName: id user: targetEntity: Acme\Entity\User joinColumn: name: ByUserID referencedColumnName: id So the "dateExecuted" field is used to compose an artificial primary key since the table has no PK. The error is: [ErrorException] Catchable Fatal Error: Object of class DateTime could not be converted to string in vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php line 1295 Exception trace: () at vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:1295 Symfony\Component\HttpKernel\Debug\ErrorHandler->handle() at n/a:n/a implode() at vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:1295 Doctrine\ORM\UnitOfWork->addToIdentityMap() at vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:1125 Doctrine\ORM\UnitOfWork->scheduleForInsert() at vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:810 Doctrine\ORM\UnitOfWork->persistNew() at vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:1540 Doctrine\ORM\UnitOfWork->doPersist() at vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:1501 Doctrine\ORM\UnitOfWork->persist() at vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php:565 ... In my composer.json, the relevant versions of Doctrine are: { { " package " : "doctrine/common" , "version" : "2.3.0-RC1" }, { " package " : "doctrine/dbal" , "version" : "2.3.x-dev" , "source-reference" : "239630b61f03f39d198441eced1bfffb7b0e61d1" , "commit-date" : "1346866589" }, { " package " : "doctrine/orm" , "version" : "2.3.0-RC1" }, } Is this a bug or am I misusing this?
        Hide
        Christophe Coevoet added a comment -

        this has never been supported. Doctrine 2 has always required id fields to be castable as string, which is not the case of a DateTime object

        Show
        Christophe Coevoet added a comment - this has never been supported. Doctrine 2 has always required id fields to be castable as string, which is not the case of a DateTime object
        Show
        Jonathan Ingram added a comment - But didn't the PR https://github.com/doctrine/doctrine2/pull/74 get merged in https://github.com/doctrine/doctrine2/commit/98bc3c4e402731fae8716e99c7c19d5e0ff7b60a ? And you can see the use of @Id in the test cases, e.g. https://github.com/doctrine/doctrine2/commit/98bc3c4e402731fae8716e99c7c19d5e0ff7b60a#L2R78 ?

          People

          • Assignee:
            Guilherme Blanco
            Reporter:
            Michael Ridgway
          • Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: