We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 11793
    • 49 Posts
    I’ve run into a strange problem. So strange that I’m having to devise a workaround for now since I have no time to investigate it further.

    My database table has a date field that allows null, with default null.

    XPDO can insert and update to the field, but when you try to set the value back to an empty string (via a form interface), it refuses. It keeps the existing value.

    I’ve followed the code all the way thru MakeForm (yeah yeah I know) and here’s where the rubber meets the road:

    $object->fromArray($classVal, '', true);


    It’s calling the fromArray() method of an XPDOObject.

    I’ve echo’d out $classVal and the desired value is indeed empty, so I can only assume that there is some perfect storm of date field type and allow null preventing the value from being set back to empty.

    My workaround involves doing a force update to NULL, since even an empty string will not do the trick:
    $xpdoObject->set($field,NULL);
    $xpdoObject->save();


    for each date field (no biggie).

    I wish I had time to dive into this to see what aspect is causing the problem.

    Oh, and I know you’re going to want to see the XML:

    <field key="field_name" dbtype="date" phptype="date" null="true" />


    UPDATE:

    I ended up working waaay around the problem by turning all empty $_POST vars into true NULL values. Fingers crossed...
    • I am not a database guru, I do know that empty is not at all the same thing as NULL. While there may be something else going on here, I suspect that expecting the same behavior from the two is at the heart of the problem.
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
      • Correct, an empty string is not a valid MySQL datetime value. It needs to be NULL or ’0000-00-00 00:00:00’.