Skip to content

Instantly share code, notes, and snippets.

@rdeutz
Created February 27, 2014 15:31
Show Gist options
  • Save rdeutz/9252368 to your computer and use it in GitHub Desktop.
Save rdeutz/9252368 to your computer and use it in GitHub Desktop.
FOF: Don't overwrite values in the db when fields are not within the form
class whatModelever extends FOFModel
{
.....
/**
* This method runs before the $data is saved to the $table. Return false to
* stop saving.
*
* @param array &$data The data to save
* @param FOFTable &$table The table to save the data to
*
* @return boolean Return false to prevent saving, true to allow it
*/
protected function onBeforeSave(&$data, &$table)
{
$result = parent::onBeforeSave($data, $table);
/*
next lines make sure that not in a form existing fields are
are not set to null
*/
if ($result && !$this->_isNewRecord)
{
// Get the form if there is any
$form = $this->getForm($data, false);
if ($form instanceof FOFForm)
{
$formdata = $this->input->getData();
$key = $table->getKeyName();
$SavedRecord = $this->getTable();
$SavedRecord->load($table->$key);
// get the fields
$fieldset = $form->getFieldset();
foreach ($fieldset as $nfield => $fldset)
{
if (!array_key_exists($nfield, $formdata))
{
$oldValue = $SavedRecord->$nfield;
$table->$nfield = $oldValue;
$data[$nfield] = $oldValue;
}
}
}
}
return true;
}
.....
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment