I created a new complex node type. It represents an aggregate object, which consists of several sub-objects, so the data is spread over several tables in the database. I construct my node object in hook_load like this:
function myNode_load($node)
{
// switch database
db_set_active('myDB');
// create my object
$myObject = db_fetch_object( db_query( ... ) );
$myObject->subObject1 = db_fetch_object( db_query( ... ) );
$myObject->subObject2 = db_fetch_object( db_query( ... ) );
...
// switch back database
db_set_active('default');
// append myObject to node-object, and return the node
$node->myObject = $myObject;
return $node;
}
Note, that I return $node instead of $myObject. This way $myObject is not flattened into a set of properties an merged with $node, but it remains as an object that is attached to $node. This way I can use the object in other following hooks like I constructed it. For instance in hook_form:
$form['myObject']['subObject1']['latitude'] = array
(
'#type' => 'textfield',
'#title' => t('latitude'),
'#default_value' => $node->myObject->subObject1->latitude
);
My problem is now, that $myObject is lost after submiting the form. As far as I understand, node_form_submit($form_id, $edit)
in node.module creates a new node-object out of the data submited in the form elements. Therefore $myObject is lost and not attached to the $node object which is passed to hook_validate, hook_update and so on... The following doesn't work: