1. We suggest you do NOT update to Joomla 3.8.10 until we can resolve an issue it causes with caching in Fabrik. If you do install it, you'll need to disable Joomla's "System Cache" in the global System settings.
  2. Apologies for the recent server outage, a planned migration by our host provider to a new location turned into a bit of a nightmare.

Question about upsert value of empty form field

Discussion in 'Community' started by econveyors, Jul 12, 2018.

  1. econveyors

    econveyors New Member

    Level: Professional
    Here's my situation and the functionality I'm looking to accomplish:
    - I have a form that uses the upsert plugin to copy the form data to a second table.
    - For one specific field, I only want it to overwrite the second table's data if the form field contains a value.
    - If the form field is empty, I want the data that's already in the second table's field to remain untouched.

    I've tried using just the placeholder as well as several versions of code to check if the form field contains data, but what continues to happen is this:
    - When the form field contains a value, the field in the second table is successfully updated with the form field's value.
    - When the form field is empty, the field in the second table is updated, but it's blank... I'm assuming because the form field was left blank.

    Here's what I've tried. This code is being inserted into the "Value" field in the Upsert plugin and evaluated as PHP (obviously except for when I tried just the placeholder.)

    Placeholder
    Code (Text):
    {tablename___elementname}
    Example 1
    Code (Text):
    $formField = $formModel->fullFormData['tablename___elementname'];
    if(!empty($formField)):
        return $formField;
    endif;
    Example 2
    Code (Text):
    $formField = $formModel->fullFormData['tablename___elementname'];
    if(isset($formField)):
        return $formField;
    endif;
    Example 3
    Code (Text):
    $formField = $formModel->fullFormData['tablename___elementname'];
    if(isset($formField) && !empty($formField)):
        return $formField;
    endif;
    Example 4
    Code (Text):
    $formField = $formModel->fullFormData['tablename___elementname'];
    if($formField != ''):
        return $formField;
    endif;
    This feels like it should be pretty simple, but for some reason I can't figure out what I'm missing.

    Thanks!
     
  2. cheesegrits

    cheesegrits Support Gopher Staff Member

    Level: Professional
    Yeah, the way it works at the moment is if the value is empty, we use the default.

    What you could try is edit ...

    plugins/fabrik_form/upsert/upsert.php

    ... at line 165, modify the code to check to see if the returned value is false ...

    Code (Text):

                if ($upsert->upsert_eval_value[$i] === '1')
                {
                    $res = FabrikHelperHTML::isDebug() ? eval($v) : @eval($v);
                    FabrikWorker::logEval($res, 'Eval exception : upsert : ' . $v . ' : %s');

                    // if the eval'ed code returned false, skip this
                    if ($res === false)
                    {
                         continue;
                    }

                    $v = $res;
                }
     
    I'm not 100% what would happen if this is the only field being updated, so we wind up with no values to update, but this would at least be a start, even if it needs to be tweaked a little.

    Then in your eval'ed code, return false if you don't want to update.

    if this works, I'll implement it in github.
     
  3. econveyors

    econveyors New Member

    Level: Professional
    Thank you hugh!
    This worked perfectly.

    I made the code change to upsert.php

    And changed my upsert value to:

    Code (Text):
    $formField = $formModel->fullFormData['tablename___elementname'];
    if(!empty($formField)):
        return $formField;
    else:
       return false;
    endif;
    I'm updating more than one field, so I can't speak to how it would work with only one field, but it does exactly what I needed in my case.

    Thank you!!
     
  4. cheesegrits

    cheesegrits Support Gopher Staff Member

    Level: Professional
    Can you do me a favor and test it with only one, see if it blows up?

    -- hugh
     
  5. econveyors

    econveyors New Member

    Level: Professional
    Ok.

    My form has quite a few plugins going on, so I created a copy of my site to test upserting a single field with this method.

    I turned off all other form plugins and removed all fields from the Upsert plugin except for this one.

    I tested editing the record with a value in the form field and with the form field left empty.

    When there's a value in the form field, the record in the upserted table is updated as expected.

    When the form field is empty, the record in the upserted table is not updated, which is expected. I can see the correct value in the database. But, there is an error thrown on the screen.

    You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE `upsertedtablename`.`foreign_key_element` = '###'' at line 3

    Hope this helps!
     
  6. cheesegrits

    cheesegrits Support Gopher Staff Member

    Level: Professional
    Thanks.

    Yeah, I thought that might be the case. If there's no fields to update, it'll try and execute a query like "UPDATE foo WHERE fk=123" or "INSERT foo", with no "SET something=whatever".

    I think I can work round that.

    -- hugh
     
  7. cheesegrits

    cheesegrits Support Gopher Staff Member

    Level: Professional
    OK, at line 82, add this test ...

    Code (Text):

            $fields = $this->upsertData($upsertRowExists);

            // make sure we have at least one field to upsert
            if (empty($fields))
            {
                return true;
            }
     
    -- hugh
     
  8. econveyors

    econveyors New Member

    Level: Professional
    Tested the additional code on both forms, with only a single field upserted and with multiple fields upserted.

    Everything works as expected and no errors thrown.
     
  9. cheesegrits

    cheesegrits Support Gopher Staff Member

    Level: Professional
  10. econveyors

    econveyors New Member

    Level: Professional
    Great! Thanks for all your help!
     
    cheesegrits likes this.

Share This Page