function testSetChangedFlagsFromLast3()
 {
     $lastHistory = new TagHistoryModel();
     $lastHistory->setFromDataBaseRow(array('tag_id' => 1, 'title' => 'Cat', 'description' => 'no dogs allowed', 'user_account_id' => 1, 'created_at' => '2014-02-01 10:00:00', 'is_deleted' => 0, 'title_changed' => 0, 'description_changed' => 0, 'url_changed' => 0, 'twitter_username_changed' => 0, 'is_deleted_changed' => 0));
     $tagHistory = new TagHistoryModel();
     $tagHistory->setFromDataBaseRow(array('tag_id' => 1, 'title' => 'Cat', 'description' => 'no dogs allowed', 'user_account_id' => 1, 'created_at' => '2014-02-01 10:00:00', 'is_deleted' => 1, 'title_changed' => 0, 'description_changed' => 0, 'url_changed' => 0, 'twitter_username_changed' => 0, 'is_deleted_changed' => 0));
     $tagHistory->setChangedFlagsFromLast($lastHistory);
     $this->assertEquals(false, $tagHistory->getTitleChanged());
     $this->assertEquals(false, $tagHistory->getDescriptionChanged());
     $this->assertEquals(true, $tagHistory->getIsDeletedChanged());
     $this->assertEquals(false, $tagHistory->getIsNew());
 }
 public function ensureChangedFlagsAreSet(TagHistoryModel $tagHistory)
 {
     global $DB;
     // do we already have them?
     if (!$tagHistory->isAnyChangeFlagsUnknown()) {
         return;
     }
     // load last.
     $stat = $DB->prepare("SELECT * FROM tag_history WHERE tag_id = :id AND created_at < :at " . "ORDER BY created_at DESC");
     $stat->execute(array('id' => $tagHistory->getId(), 'at' => $tagHistory->getCreatedAt()->format("Y-m-d H:i:s")));
     // Apply what we know
     if ($stat->rowCount() == 0) {
         $tagHistory->setChangedFlagsFromNothing();
     } else {
         while ($tagHistory->isAnyChangeFlagsUnknown() && ($lastHistoryData = $stat->fetch())) {
             $lastHistory = new TagHistoryModel();
             $lastHistory->setFromDataBaseRow($lastHistoryData);
             $tagHistory->setChangedFlagsFromLast($lastHistory);
         }
     }
     // Save back to DB
     $sqlFields = array();
     $sqlParams = array('id' => $tagHistory->getId(), 'created_at' => $tagHistory->getCreatedAt()->format("Y-m-d H:i:s"), 'is_new' => $tagHistory->getIsNew() ? 1 : 0);
     if ($tagHistory->getTitleChangedKnown()) {
         $sqlFields[] = " title_changed = :title_changed ";
         $sqlParams['title_changed'] = $tagHistory->getTitleChanged() ? 1 : -1;
     }
     if ($tagHistory->getDescriptionChangedKnown()) {
         $sqlFields[] = " description_changed = :description_changed ";
         $sqlParams['description_changed'] = $tagHistory->getDescriptionChanged() ? 1 : -1;
     }
     if ($tagHistory->getIsDeletedChangedKnown()) {
         $sqlFields[] = " is_deleted_changed = :is_deleted_changed ";
         $sqlParams['is_deleted_changed'] = $tagHistory->getIsDeletedChanged() ? 1 : -1;
     }
     $statUpdate = $DB->prepare("UPDATE tag_history SET " . " is_new = :is_new, " . implode(" , ", $sqlFields) . " WHERE tag_id = :id AND created_at = :created_at");
     $statUpdate->execute($sqlParams);
 }