Fixed \XF\Mvc\Entity\Entity::saveIfChanged does not reset _writePending

DragonByte Tech

Well-known member
Affected version
2.3.4
Problem: If you call $entity->preSave(), \XF\Mvc\Entity\Entity::$_writePending is set to true. If you subsequently call $entity->saveIfChanged(), and no changes are detected, you cannot write to the entity later, unless you manually call $entity->reset().

Proof of concept:
PHP:
$user = \XF::em()->find(\XF\Entity\User::class, 1);
$user->username = 'Admin'; // Set this to whatever the user name of userId 1 is

if (!$user->preSave())
{
    throw new \XF\PrintableException("error");
}

$user->saveIfChanged();

$user->email = 'xfbug@example.com'; // This will fail with LogicException: Attempted to set 'email' while a save was pending without forceSet

Suggested fix:
Diff:
Index: src/XF/Mvc/Entity/Entity.php
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/src/XF/Mvc/Entity/Entity.php b/src/XF/Mvc/Entity/Entity.php
--- a/src/XF/Mvc/Entity/Entity.php
+++ b/src/XF/Mvc/Entity/Entity.php
@@ -1362,6 +1362,8 @@
     {
         if (!$this->_newValues && $this->isUpdate())
         {
+            $this->_writePending = false;
+           
             $saved = false;
             return true;
         }
 
Thank you for reporting this issue, it has now been resolved. We are aiming to include any changes that have been made in a future XF release (2.3.5).

Change log:
Properly reset write-pending status when calling `Entity::saveIfChanged`
There may be a delay before changes are rolled out to the XenForo Community.
 
Back
Top Bottom