Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

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

Closed
wants to merge 1 commit into from

5 participants

cedric lombardot doctrinebot Marco Pivetta Benjamin Eberlei Eduardo Oliveira
cedric lombardot

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

cedric lombardot 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
Collaborator

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

Marco Pivetta
Collaborator

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

cedric lombardot

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

Marco Pivetta
Collaborator

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

cedric lombardot

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

Marco Pivetta
Collaborator

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

Benjamin Eberlei
Owner

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

Eduardo Oliveira

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

Marco Pivetta
Collaborator

@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

Eduardo Oliveira

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

Benjamin Eberlei
Owner

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

Marco Pivetta
Collaborator

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

Eduardo Oliveira

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

Benjamin Eberlei
Owner

what version of ORM are you using?

Eduardo Oliveira

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

Showing 1 unique commit by 1 author.

Jan 15, 2013
cedric lombardot 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
This page is out of date. Refresh to see the latest.

Showing 1 changed file with 4 additions and 0 deletions. Show diff stats Hide diff stats

  1. +4 0 lib/Doctrine/ORM/UnitOfWork.php
4 lib/Doctrine/ORM/UnitOfWork.php
@@ -2455,6 +2455,10 @@ public function createEntity($className, array $data, &$hints = array())
2455 2455 $id[$fieldName] = isset($class->associationMappings[$fieldName])
2456 2456 ? $data[$class->associationMappings[$fieldName]['joinColumns'][0]['name']]
2457 2457 : $data[$fieldName];
  2458 +
  2459 + if ($id[$fieldName] instanceof \DateTime) {
  2460 + $id[$fieldName] = $id[$fieldName]->getTimestamp();
  2461 + }
2458 2462 }
2459 2463
2460 2464 $idHash = implode(' ', $id);

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.