Skip to content

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

Closed
wants to merge 1 commit into from

5 participants

@cedriclombardot

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
b4dc608
@doctrinebot

Hello,

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:

http://doctrine-project.org/jira/browse/DDC-2242

@Ocramius
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.

@cedriclombardot

Its not about comparing same dates, it's only because at this line do https://github.com/cedriclombardot/doctrine2/blob/b4dc6080b3dd95d2ba23b3abe8cff70668acafcf/lib/Doctrine/ORM/UnitOfWork.php#L2464 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

@Ocramius
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.

@cedriclombardot

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

@Ocramius
Doctrine member

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

@beberlei
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
@eb-eoliveira

@beberlei I'm using simple derived entity: http://docs.doctrine-project.org/en/latest/tutorials/composite-primary-keys.html#use-case-2-simple-derived-identity

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

Publisher:

     * @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?

@Ocramius
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 http://stackoverflow.com/questions/15080573/doctrine-2-orm-datetime-field-in-identifier, which is the problem described by @cedriclombardot

@eb-eoliveira

@Ocramius thanks for the explanation, I suggest that at documentation (http://docs.doctrine-project.org/en/latest/tutorials/composite-primary-keys.html#use-case-2-simple-derived-identity) should be added a note about it.

@beberlei
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?

@Ocramius
Doctrine member

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

@eb-eoliveira

@beberlei

Account Publisher: https://gist.github.com/eb-eoliveira/5413730

Account Entity: https://gist.github.com/eb-eoliveira/5413718

Stack trace: https://gist.github.com/eb-eoliveira/5413761

On stack trace you can see that the last line of code before enter doctrine code is:
/home/www/eb/platform/src/EB/Core/KernelBundle/Doctrine/ORM/EntityRepository.php:158

You can check it: https://gist.github.com/eb-eoliveira/5413804

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

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

@beberlei
Doctrine member

what version of ORM are you using?

@eb-eoliveira

2.4.0-BETA1

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 (http://www.doctrine-project.org/jira/browse/DDC-2306) 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.