Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DDC-3377: DateTime columns cannot be used with @Id #4178

Closed
doctrinebot opened this issue Nov 6, 2014 · 13 comments
Closed

DDC-3377: DateTime columns cannot be used with @Id #4178

doctrinebot opened this issue Nov 6, 2014 · 13 comments
Assignees

Comments

@doctrinebot
Copy link

Jira issue originally created by user cverges-ch:

DateTimeIdTest.php:

use Doctrine\ORM\Mapping as ORM;

/****
 * @ORM\Table(name="datetime*id*test")
 * @ORM\Entity
 */
class DateTimeIdTest
{
    /****
     * @ORM\Column(name="when", type="datetime")
     * @ORM\Id
     */
    public $when;

    public function **construct()
    {
        $this->when = new \DateTime();
    }
}

test.php:

require_once('DateTimeIdTest.php');

$entity = new DateTimeIdTest();
$entityManager->persist($entity);

Output:

Object of class DateTime could not be converted to string

/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:1359
/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:1172
/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:864
/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:1635
/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:1591
/vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php:624
/app/cache/test/jms*diextra/doctrine/EntityManager*545bc3b10fd93.php:94
test.php:4

I was able to debug this down to where the implode() call in Doctrine\ORM\UnitOfWork::addToIdentityMap() is what is triggering this problem. It attempts to convert all contained entities in the Doctrine\ORM\UnitOfWork::entityIdentifiers array to strings. For a DateTime object, the DateTime::format() function should be used instead of relying on **toString().

@doctrinebot
Copy link
Author

Comment created by cverges-ch:

There is a portable workaround where you create a string-based shadow key that gets updated by Doctrine events:

DateTimeIdTest.php:

use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\Events as Events;

/****
 * @ORM\Table(name="datetime*id*test")
 * @ORM\Entity
 * @ORM\HasLifecycleCallbacks
 */
class DateTimeIdTest
{
    public $when;

    /****
     * @ORM\Column(name="when", type="string")
     * @ORM\Id
     */
    public $whenKey;

    public function **construct()
    {
        $this->when = new \DateTime();
    }

    /****
     * @Events\PrePersist
     * @Events\PreUpdate
     */
    public function syncWhenToWhenKey(LifecycleEventArgs $event)
    {
        $entityManager = $event->getEntityManager();
        $connection = $entityManager->getConnection();
        $platform = $connection->getDatabasePlatform();

        $this->whenKey = $when->format($platform->getDateTimeFormatString());
    }

    /****
     * @Events\PostLoad
     */
    public function syncWhenKeyToWhen(LifecycleEventArgs $event)
    {
        $entityManager = $event->getEntityManager();
        $connection = $entityManager->getConnection();
        $platform = $connection->getDatabasePlatform();

        $this->when = \DateTime::createFromFormat($platform->getDateTimeFormatString(), $this->whenKey);
    }
}

@doctrinebot
Copy link
Author

Issue was closed with resolution "Duplicate"

@peter-gribanov
Copy link
Contributor

Will this bug fix?

@Ocramius
Copy link
Member

@peter-gribanov I already marked this as duplicate

@peter-gribanov
Copy link
Contributor

@Ocramius Thanks. I noticed, but where is the original issue? Is it only in your Jira?

@Ocramius
Copy link
Member

@peter-gribanov all issues were copied over and auto-closed: you can search for them by the identifier (for example DDC-123456) in this repo

@peter-gribanov
Copy link
Contributor

@drupol
Copy link

drupol commented Dec 7, 2020

I'm also looking for a solution.

@beberlei
Copy link
Member

beberlei commented Dec 7, 2020

@drupol You could look into using Chronos for Date intead of native date types. They have a __toString and that should make this work: https://packagist.org/packages/warhuhn/chronos-doctrine

@drupol
Copy link

drupol commented Dec 7, 2020

Thanks @beberlei, I will give it a try again.

@drupol
Copy link

drupol commented Dec 7, 2020

Dear @beberlei , I tried here: https://github.com/drupol/doctrine-date-as-id/pull/2 but apparently, it's the same issue.

@beberlei
Copy link
Member

beberlei commented Dec 7, 2020

Please read the error csrefully, your code still has a DateTime object there you need a Chronos instance

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants