Autosave Custom Fields in Parameters

This pair of functions is designed to give you an easy way of saving / loading custom Variable content to a Midgard Object.

Overview

The basic idea is to define a list of variable names that are member of the object in question, along with a pair of functions that automagically saves or loads this values into Midgard Parameters. The real advantage of this function comes with the extended Midgard Classes. You just define a set of $var's, then call those functions with a statically defined array of the variables you'd like to save.

Parameter Syntax

Both forms of the function take three parameters:

The first $array is an array containing the names of the variables you like to save. The Save Function checks, whether any of this variables gets longer then 255 characters before anything is done, so if you get an false on save and mgd_errstr doesn't report anything, this might be the cause of the failure.

The Reference in $object points to the object in question which owns the variables you want to save. If you call this function directly from this object, this will usually be $this.

The last required information, $domain is the domain in which the parameters should be saved in.

Return Values

The usual false on failure, true on success. Keep an eye on mgd_errstr() if anyhting goes wrong.

Code

<?php
function mgd_save_custom_fields_param ($array, &$object, $domain) {

  foreach (
$array as $var) {
    eval (
"\$result = strlen(\$object->$var);");
    if (
$result > 255)
      return
false;
  }

  foreach (
$array as $var) {
    eval (
"\$var_data = \$object->$var;");
    if (
strlen($var_data) > 0)
      return
$object->parameter($domain,$var,$var_data);
  }

  return
true;
}


function
mgd_load_custom_fields_param ($array, &$object, $domain) {

  foreach (
$array as $var) {
    
$result = $object->parameter($domain, $var);
    if (
$result)
      eval (
"\$object->$var = \$result;");
    else
      eval (
"\$object->$var = \"\";");
  }

  return
true;
}
?>