DDC-1310: Datetime fields merge bug #1643

Open
opened 2026-01-22 13:20:52 +01:00 by admin · 0 comments
Owner

Originally created by @doctrinebot on GitHub (Aug 1, 2011).

Originally assigned to: @beberlei on GitHub.

Jira issue originally created by user slaci:

Merge compares Datetime objects by ===; so if you assign a new datetime object to a datetime field (which contains the same date in the db already), then merge will issue an UPDATE on that field (because the objects hashes don't match for ===). The expected behavior is to assume it is unchanged, so no update.

Solution: Datetime objects should be compared by == (or by $dt->format('something') maybe)

Easy to reproduce:

<?php
/*** @Entity **/
class EgEntity {
  /*** @Id **/
  private $id;
  /**** @Column(type="datetime")
  private meeting;
  //other fields...

  public function setId($id) {
    $this->id = (int)$id;
  }

  public function setMeeting(Datetime $dt) {
    $this->meeting = $dt;
  }
}
$a = new EgEntity();
$a->setId(1);
$a->setMeeting(new Datetime('2011-08-01'));
$em->merge($a); //this will issue an UPDATE even if id 1 is 2011-08-01 already
?>
Originally created by @doctrinebot on GitHub (Aug 1, 2011). Originally assigned to: @beberlei on GitHub. Jira issue originally created by user slaci: Merge compares Datetime objects by ===; so if you assign a new datetime object to a datetime field (which contains the same date in the db already), then merge will issue an UPDATE on that field (because the objects hashes don't match for ===). The expected behavior is to assume it is unchanged, so no update. Solution: Datetime objects should be compared by == (or by $dt->format('something') maybe) Easy to reproduce: ``` <?php /*** @Entity **/ class EgEntity { /*** @Id **/ private $id; /**** @Column(type="datetime") private meeting; //other fields... public function setId($id) { $this->id = (int)$id; } public function setMeeting(Datetime $dt) { $this->meeting = $dt; } } $a = new EgEntity(); $a->setId(1); $a->setMeeting(new Datetime('2011-08-01')); $em->merge($a); //this will issue an UPDATE even if id 1 is 2011-08-01 already ?> ```
admin added the Bug label 2026-01-22 13:20:52 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#1643