function update_contact($root) { require_once './contacts_schema.inc.php'; require_once './db_config.inc.php'; require_once 'SDO/DAS/Relational.php'; $dbh = new PDO(PDO_DSN, DATABASE_USER, DATABASE_PASSWORD); $das = new SDO_DAS_Relational($table_schema, 'contact', array($address_reference)); $das->applyChanges($dbh, $root); }
assert($company2->name == 'Acme'); assert($company2->employee_of_the_month === null); /************************************************************************************* * Force a change in the databse behind the back of SDO *************************************************************************************/ $dbh2 = new PDO(PDO_DSN, DATABASE_USER, DATABASE_PASSWORD); $count = $dbh2->exec("update company set name='ForcedNameChange' where name='Acme';"); echo "Update the company name behind the scenes\n"; echo "Number of rows updated = " . $count . "\n"; /************************************************************************************* * Update the SDO version of the record and and write it back again. * The collision detection test should fire here *************************************************************************************/ try { $company2->name = 'MegaCorp'; $das->applyChanges($dbh, $root); echo "This test should have thrown an exception indicating a collision was detected but nothing happened\n"; } catch (SDO_Exception $e) { echo "Expected notification of collision detection got... \n\n"; print "getMessage(): " . $e->getMessage() . "\n\n"; } /************************************************************************************* * Find it again under its old name, and delete it. * Just for a change, re-use the PDO database handle and prepared statement. *************************************************************************************/ $name = 'ForcedNameChange'; $root = $das->executePreparedQuery($dbh, $pdo_stmt, array($name), array('company.name', 'company.id', 'company.employee_of_the_month')); $company3 = $root['company'][0]; echo "Looked for ForcedNameChange and found company with name = " . $company3->name . " and id " . $company3->id . "\n"; assert($company3->name == 'ForcedNameChange'); unset($root['company'][0]);
/************************************************************** * Get and initialise a DAS with the metadata ***************************************************************/ try { $das = new SDO_DAS_Relational($database_metadata, 'company', $SDO_reference_metadata); } catch (SDO_DAS_Relational_Exception $e) { echo "SDO_DAS_Relational_Exception raised when trying to create the DAS."; echo "Probably something wrong with the metadata."; echo "\n" . $e->getMessage(); exit; } /************************************************************** * Create three company data objects under the same root ***************************************************************/ $root = $das->createRootDataObject(); $acme = $root->createDataObject('company'); $acme->name = "Acme"; $megacorp = $root->createDataObject('company'); $megacorp->name = 'MegaCorp'; $ultracorp = $root->createDataObject('company'); $ultracorp->name = 'UltraCorp'; $dbh = new PDO(PDO_DSN, DATABASE_USER, DATABASE_PASSWORD); try { $das->applyChanges($dbh, $acme); echo "Companies Acme, MegaCorp and UltraCorp have been written to the database\n"; } catch (SDO_DAS_Relational_Exception $e) { echo "\nSDO_DAS_Relational_Exception raised when trying to apply changes."; echo "\nProbably something wrong with the data graph."; echo "\n" . $e->getMessage(); exit; }