/** * Save the object to the database (and optionally solr) based on the structure of the object * Takes care of determining whether or not the object is new or not. * * @param $dataObject * @param $form */ static function saveObject($structure, $dataType) { global $logger; //Check to see if we have a new object or an exiting object to update $object = new $dataType(); DataObjectUtil::updateFromUI($object, $structure); $primaryKeySet = false; foreach ($structure as $property) { if (isset($property['primaryKey']) && $property['primaryKey'] == true) { if (isset($object->{$property}['property']) && !is_null($object->{$property}['property']) && strlen($object->{$property}['property']) > 0) { $object = new $dataType(); $object->{$property}['property'] = $object->{$property}['property']; if ($object->find(true)) { $logger->log("Loaded existing object from database", PEAR_LOG_DEBUG); } else { $logger->log("Could not find existing object in database", PEAR_LOG_ERR); } //Reload from UI DataObjectUtil::updateFromUI($object, $structure); $primaryKeySet = true; break; } } } $validationResults = DataObjectUtil::validateObject($structure, $object); $validationResults['object'] = $object; if ($validationResults['validatedOk']) { //Check to see if we need to insert or update the object. //We can tell which to do based on whether or not the primary key is set if ($primaryKeySet) { $result = $object->update(); $validationResults['saveOk'] = $result == 1; } else { $result = $object->insert(); $validationResults['saveOk'] = $result; } if (!$validationResults['saveOk']) { //TODO: Display the PEAR error (in certain circumstances only?) $error =& PEAR_Singleton::getStaticProperty('DB_DataObject', 'lastError'); if (isset($error)) { $validationResults['errors'][] = 'Save failed ' . $error->getMessage(); } else { $validationResults['errors'][] = 'Save failed'; } } } return $validationResults; }
function updateFromUI($object, $structure) { require_once ROOT_DIR . '/sys/DataObjectUtil.php'; DataObjectUtil::updateFromUI($object, $structure); $validationResults = DataObjectUtil::validateObject($structure, $object); return $validationResults; }