Skip to content

Update lib/Doctrine/ORM/UnitOfWork.php #550

wants to merge 1 commit into from

5 participants


When the pk contains a DateTime object doctrine fail to create the index, because implode call __toString wich doesn't exists in \DateTime object

@cedriclombardot cedriclombardot Update lib/Doctrine/ORM/UnitOfWork.php
When the pk contains a DateTime object doctrine fail to create the index, because implode call __toString wich doesn't exists in \DateTime object


thank you for positing this Pull Request. I have automatically opened an issue on our Jira Bug Tracker for you with the details of this Pull-Request. See the Link:

Doctrine member

@cedriclombardot: if the problem is comparing same datetimes, do following in your entities instead:

public function getDate()
    return clone $this->date;

public function setDate(DateTime $date)
    $this->date = clone $date;

This will ensure your instance is always different.

This patch is invalid.


Its not about comparing same dates, it's only because at this line do an implode to calculate $idHash and this implode function call automatically the __toString method of objects.

My composite PK is base on an integer column and on a datetime column and doctrine for datetime column use \DateTime object so the implode call $datetime->__toString() wich fail because in php \DateTime have no __toString method

Doctrine member

@cedriclombardot what about using an extended version of DateTime that allows __toString then? We're stuffed with these workarounds, it's not really a solution imo.


And how you can override all instance of \DateTime to enable a __toString ?

Doctrine member

@cedriclombardot: custom type and setters/getters that handle the conversion

Doctrine member

@cedriclombardot you can implement a Doctrine\DBAL\Types\Type subtype and derive from the DateTime type, which has the date time with __toString.

Its a bit to work on, but we haven't found the perfect internal solution yet to take this work from our users.

@beberlei beberlei closed this Jan 15, 2013

@beberlei I'm using simple derived entity:

And i have the same problem, i have one entity Publisher another Account.


     * @var integer
     * @ORM\Id
     * @ORM\OneToOne(targetEntity="EB\Core\KernelBundle\Entity\Account\Account")
     * @ORM\JoinColumn(name="id", referencedColumnName="id", nullable=false)
    protected $id;

And I have exactly the same problem as described by @cedriclombardot

I can solve it, implementing __toString() in account, but doesn't sounds good, is what i need to do? In that case why is not documented?

Doctrine member

@eb-eoliveira you need the __toString() method because the UnitOfWork needs to hash the identifiers to build the identity map.

You can get a longer explanation at, which is the problem described by @cedriclombardot


@Ocramius thanks for the explanation, I suggest that at documentation ( should be added a note about it.

Doctrine member

@eb-eoliveira it should work for composite primary keys, can you paste your error message and the full stack trace into a gist or pastebin?

Doctrine member

@eb-eoliveira sorry, gave you misleading information. As @beberlei said, this should work



Account Publisher:

Account Entity:

Stack trace:

On stack trace you can see that the last line of code before enter doctrine code is:

You can check it:

$res = $this->getEntityManager()->merge($cacheRes);

All works wonderful if I add a __toString() to Account entity. I'm doing something wrong?

Doctrine member

what version of ORM are you using?



More info:

$ php composer.phar show -i | grep ^doctrine
doctrine/annotations                   v1.1               Docblock Annotations Parser
doctrine/cache                         v1.0               Caching library offering an object-oriented API for many cache backends
doctrine/collections                   v1.1               Collections Abstraction library
doctrine/common                        dev-master 33f906e Common Library for Doctrine projects
doctrine/data-fixtures                 dev-master 6924952 Data Fixtures for all Doctrine Object Managers
doctrine/dbal                          2.4.0-BETA1        Database Abstraction Layer
doctrine/doctrine-bundle               dev-master v1.2.0  Symfony DoctrineBundle
doctrine/doctrine-fixtures-bundle      dev-master 275540d Symfony DoctrineFixturesBundle
doctrine/doctrine-migrations-bundle    v1.0.0-beta1       Symfony DoctrineMigrationsBundle
doctrine/inflector                     v1.0               Common String Manipulations with regard to casing and singular/plural rules.
doctrine/lexer                         v1.0               Base library for a lexer that can be used in Top-Down, Recursive Descent Parsers.
doctrine/orm                           2.4.0-BETA1        Object-Relational-Mapper for PHP

I'm using the beta, because I got problems on serialize and unserialize ( so I updated to 2.4 beta to solve the problem.

Let me know if you need more info.

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.