Upsert with condition

Status
Not open for further replies.

Sadler

Member
Hi,

I need to run an upsert on a form submission but only when one of the other fields is not zero (it is a drop down field and can only be 1 or 0, it defaults to 0).
I am trying to achieve this using the condition on the upsert plugin, here is what I have in the condition field:

Code:
$Upsert = $formModel->formData['my_table___my_field'];
if ($Upsert > 0) {return true;}
else
{return false;}

On testing it the upsert is not running regardless of the drop down value, I am not sure that I am accessing the submitted form data correctly, I am using some code snippets I found elsewhere in the wiki.

Anyone have some experience on this to share or spot an obvious blunder.

Cheers

Burnsy
 
I think by the time upsert runs, we've already removed the tablename___ prefix from the form data (which we do when we write the data out to the database). So you can either use $formModel->formData['my_field'], or we also maintain another array of the submitted data which we leave the tablename___ prefix intact on, $formModel->formDataWithTableName['my_table___my_field'].

Also, dropdowns are usually going to be arrays in the data, so you need to account for that.

So something like ...

Code:
$myUpsert = $formModel->formDataWithTableName['my_table___my_field'];
$myUpsert = is_array($myUpsert) ? $myUpsert[0] : $myUpsert;
return (int) $myUpsert > 0;

-- hugh
 
Status
Not open for further replies.
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top