Storing arbitary variables in attachments
As Midgard parameters are only able to store up to 255 character values, you can use attachments to store serialized variables of any type and size.
Overview
The following is a very basic implementation of this concept which leaves serialization (with PHP's serialize function) and type-checking to the caller. This leaves room for improvement :-)
Note, that this class uses the debugging functions.
Syntax & return values
mgd_save_var_as_attachment() takes three arguments: the $object to attach the BLOB to, the variable $var you want to store (should be already a string) and the attachment $name. mgd_save_var_as_attachment() returns TRUE when successful, FALSE on failure.
mgd_load_var_from_attachment() takes the storage $object and the attachment's $name as arguments and returns the variable on success, otherwise FALSE.
Code
<?php
function mgd_save_var_as_attachment($object, $var, $name) {
$att = $object->getattachment($name);
if (!$att)
$att = $object->createattachment($name,
"mgd_save_var of $name",
"application/octet-stream");
if (!$att)
return false;
$h_att = $object->openattachment($name);
if (!$h_att)
{
debug_add ("Could not open Attachment.");
return false;
}
$result = fwrite ($h_att, $var);
if ($result == -1)
{
debug_add ("Save : Write failed!");
return false;
}
if (!fclose ($h_att))
{
debug_add ("Save : Failed closing Filehandle");
return false;
}
return true;
}
function mgd_load_var_from_attachment($object, $name) {
$att = $object->getattachment ($name);
if (!$att)
return false;
$h_att = $object->openattachment($name, "r");
if (!$h_att)
return false;
$stats = mgd_stat_attachment ($att->id);
$result = fread ($h_att, $stats[7]);
fclose ($h_att);
return $result;
}
?>