We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 14883 ☆ A M B ☆
    • 450 Posts
    I have fields in my table that can be null or have a value of 1. They are populated by checkboxes.

    If the field is set to 1,and the user unchecks the box when updating the form, I want to set the value of the field to null.

    I think what I need to do is

    • get all fields in the class that can be NULL
    • compare them to the list of fields that were submitted in the form
    • set the values of those fields not present in the form to NULL

    Does that sound about right?

    If so, how would I go about doing that in xpdo syntax?

      • 28215
      • 4,149 Posts
      $object->set(’field’,null);
        shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
        • 14883 ☆ A M B ☆
        • 450 Posts
        OK, so that’s how I set a given field to null. Thanks!

        I was able to answer the other part of my question myself by staring at my classname.map.inc.php file for a long time. You can use ->getFieldMeta() to determine which fields are null-capable. What I needed was to set anything null-capable AND not present in the form results (i.e., unchecked checkboxes) to null.
        $profile->fromArray($scriptProperties['fields']); // update all the fields that were present in the form
        $fieldmeta = $xpdo->getFieldMeta('Extraprofiles');  // get the field properties
        foreach ( $fieldmeta as $fieldname=>$fieldproperties) {
          if ($fieldproperties['null'] = true) { //if null is allowed for the field
             if (!(isset($scriptProperties['fields'][$fieldname]))) { //and if the field wasn't present in the form 
                 $profile->set($fieldname, null);    // explicitly set that field to null 
             }
          }
        }