<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Three-table example</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="sdodasrel.examples.two-table.html">Two-table examples</a></div> <div class="next" style="text-align: right; float: right;"><a href="sdodasrel.limitations.html">Limitations</a></div> <div class="up"><a href="sdodasrel.examples.html">Examples</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="sdodasrel.examples.three-table" class="section"> <h2 class="title">Three-table example</h2> <p class="para"> The following examples use all three tables from the company database: the company, department, and employee tables. These introduce the final piece of function not exercised by the examples above: the non-containment reference <var class="varname"><var class="varname">employee_of_the_month</var></var>. </p> <p class="para"> Like the examples above for company and department, this set of examples is intended to illustrate the full lifecycle of such a data graph. </p> <p class="para"> <div class="example" id="example-5474"> <p><strong>Example #1 One company, one department, one employee - Create</strong></p> <div class="example-contents"><p> In this example a company is created containing one department and just one employee. Note that this example clears out all three tables at the start so that the exact results of the queries can be known. </p></div> <div class="example-contents"><p> Note how once the company, department and employee have been created, the <var class="varname"><var class="varname">employee_of_the_month</var></var> property of the company can be made to point at the new employee. As this is a non-containment reference, this cannot be done until the employee object has been created within the graph. Non-containment references need to be managed carefully. For example if the employee were now deleted from under the department, it would not be correct to try to save the graph without first clearing or re-assigning the <var class="varname"><var class="varname">employee_of_the_month</var></var> property. The closure rule for SDO data graphs requires that any object pointed at by a non-containment reference must also be reachable by containment relationships. </p></div> <div class="example-contents"><p> When it comes to inserting the graph into the database, the procedure is similar to the example of inserting the company and department, but <var class="varname"><var class="varname">employee_of_the_month</var></var> introduces an extra complexity. The Relational DAS needs to insert the objects working down the tree formed by containment relationships, so company, then department, then employee. This is necessary so that it always has the auto-generated primary key of a parent on hand to include in a child row. But when the company row is inserted the employee who is employee of the month has not yet been inserted and the primary key is not known. The procedure is that after the employee record is inserted and its primary key known, a final step is performed in which the company record is updated with the employee's primary key. </p></div> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #007700">require_once </span><span style="color: #DD0000">'SDO/DAS/Relational.php'</span><span style="color: #007700">;<br />require_once </span><span style="color: #DD0000">'company_metadata.inc.php'</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">/*************************************************************************************<br />* Empty out the three tables<br />*************************************************************************************/<br /></span><span style="color: #0000BB">$dbh </span><span style="color: #007700">= new </span><span style="color: #0000BB">PDO</span><span style="color: #007700">(</span><span style="color: #0000BB">PDO_DSN</span><span style="color: #007700">,</span><span style="color: #0000BB">DATABASE_USER</span><span style="color: #007700">,</span><span style="color: #0000BB">DATABASE_PASSWORD</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$pdo_stmt </span><span style="color: #007700">= </span><span style="color: #0000BB">$dbh</span><span style="color: #007700">-></span><span style="color: #0000BB">prepare</span><span style="color: #007700">(</span><span style="color: #DD0000">'DELETE FROM COMPANY;'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$rows_affected </span><span style="color: #007700">= </span><span style="color: #0000BB">$pdo_stmt</span><span style="color: #007700">-></span><span style="color: #0000BB">execute</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$pdo_stmt </span><span style="color: #007700">= </span><span style="color: #0000BB">$dbh</span><span style="color: #007700">-></span><span style="color: #0000BB">prepare</span><span style="color: #007700">(</span><span style="color: #DD0000">'DELETE FROM DEPARTMENT;'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$rows_affected </span><span style="color: #007700">= </span><span style="color: #0000BB">$pdo_stmt</span><span style="color: #007700">-></span><span style="color: #0000BB">execute</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$pdo_stmt </span><span style="color: #007700">= </span><span style="color: #0000BB">$dbh</span><span style="color: #007700">-></span><span style="color: #0000BB">prepare</span><span style="color: #007700">(</span><span style="color: #DD0000">'DELETE FROM EMPLOYEE;'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$rows_affected </span><span style="color: #007700">= </span><span style="color: #0000BB">$pdo_stmt</span><span style="color: #007700">-></span><span style="color: #0000BB">execute</span><span style="color: #007700">();<br /><br /></span><span style="color: #FF8000">/*************************************************************************************<br />* Create a tiny but complete company.<br />* The company name is Acme.<br />* There is one department, Shoe.<br />* There is one employee, Sue.<br />* The employee of the month is Sue.<br />*************************************************************************************/<br /></span><span style="color: #0000BB">$das </span><span style="color: #007700">= new </span><span style="color: #0000BB">SDO_DAS_Relational </span><span style="color: #007700">(</span><span style="color: #0000BB">$database_metadata</span><span style="color: #007700">,</span><span style="color: #DD0000">'company'</span><span style="color: #007700">,</span><span style="color: #0000BB">$SDO_containment_metadata</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$dbh </span><span style="color: #007700">= new </span><span style="color: #0000BB">PDO</span><span style="color: #007700">(</span><span style="color: #0000BB">PDO_DSN</span><span style="color: #007700">,</span><span style="color: #0000BB">DATABASE_USER</span><span style="color: #007700">,</span><span style="color: #0000BB">DATABASE_PASSWORD</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$root </span><span style="color: #007700">= </span><span style="color: #0000BB">$das </span><span style="color: #007700">-> </span><span style="color: #0000BB">createRootDataObject</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$acme </span><span style="color: #007700">= </span><span style="color: #0000BB">$root </span><span style="color: #007700">-> </span><span style="color: #0000BB">createDataObject</span><span style="color: #007700">(</span><span style="color: #DD0000">'company'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$acme </span><span style="color: #007700">-> </span><span style="color: #0000BB">name </span><span style="color: #007700">= </span><span style="color: #DD0000">"Acme"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$shoe </span><span style="color: #007700">= </span><span style="color: #0000BB">$acme </span><span style="color: #007700">-> </span><span style="color: #0000BB">createDataObject</span><span style="color: #007700">(</span><span style="color: #DD0000">'department'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$shoe </span><span style="color: #007700">-> </span><span style="color: #0000BB">name </span><span style="color: #007700">= </span><span style="color: #DD0000">'Shoe'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$shoe </span><span style="color: #007700">-> </span><span style="color: #0000BB">location </span><span style="color: #007700">= </span><span style="color: #DD0000">'A-block'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$sue </span><span style="color: #007700">= </span><span style="color: #0000BB">$shoe </span><span style="color: #007700">-> </span><span style="color: #0000BB">createDataObject</span><span style="color: #007700">(</span><span style="color: #DD0000">'employee'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$sue </span><span style="color: #007700">-> </span><span style="color: #0000BB">name </span><span style="color: #007700">= </span><span style="color: #DD0000">'Sue'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$acme </span><span style="color: #007700">-> </span><span style="color: #0000BB">employee_of_the_month </span><span style="color: #007700">= </span><span style="color: #0000BB">$sue</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$das </span><span style="color: #007700">-> </span><span style="color: #0000BB">applyChanges</span><span style="color: #007700">(</span><span style="color: #0000BB">$dbh</span><span style="color: #007700">, </span><span style="color: #0000BB">$root</span><span style="color: #007700">);<br /><br />echo </span><span style="color: #DD0000">"Wrote back Acme with one department and one employee\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> <p class="para"> <div class="example" id="example-5475"> <p><strong>Example #2 One company, one department, one employee - Retrieve and update</strong></p> <div class="example-contents"><p> The SQL statement passed to the Relational DAS is this time an inner join that retrieves data from all three tables. Otherwise this example introduces nothing that has not appeared in a previous example. </p></div> <div class="example-contents"><p> The graph is updated by the addition of a new department and employee and some alterations to the name properties of the existing objects in the graph. The combined changes are then written back. The Relational DAS will process and apply an arbitrary mixture of additions, modifications and deletions to and from the data graph. </p></div> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #007700">require_once </span><span style="color: #DD0000">'SDO/DAS/Relational.php'</span><span style="color: #007700">;<br />require_once </span><span style="color: #DD0000">'company_metadata.inc.php'</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">/*************************************************************************************<br />* Find the company again and change various aspects.<br />* Change the name of the company, department and employee.<br />* Add a second department and a new employee.<br />* Change the employee of the month.<br />*************************************************************************************/<br /></span><span style="color: #0000BB">$das </span><span style="color: #007700">= new </span><span style="color: #0000BB">SDO_DAS_Relational </span><span style="color: #007700">(</span><span style="color: #0000BB">$database_metadata</span><span style="color: #007700">,</span><span style="color: #DD0000">'company'</span><span style="color: #007700">,</span><span style="color: #0000BB">$SDO_containment_metadata</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$dbh </span><span style="color: #007700">= new </span><span style="color: #0000BB">PDO</span><span style="color: #007700">(</span><span style="color: #0000BB">PDO_DSN</span><span style="color: #007700">,</span><span style="color: #0000BB">DATABASE_USER</span><span style="color: #007700">,</span><span style="color: #0000BB">DATABASE_PASSWORD</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$root </span><span style="color: #007700">= </span><span style="color: #0000BB">$das</span><span style="color: #007700">-></span><span style="color: #0000BB">executeQuery</span><span style="color: #007700">(</span><span style="color: #0000BB">$dbh</span><span style="color: #007700">,<br /> </span><span style="color: #DD0000">"select c.id, c.name, c.employee_of_the_month, d.id, d.name, e.id, e.name " </span><span style="color: #007700">.<br /> </span><span style="color: #DD0000">"from company c, department d, employee e " </span><span style="color: #007700">.<br /> </span><span style="color: #DD0000">"where e.dept_id = d.id and d.co_id = c.id and c.name='Acme'"</span><span style="color: #007700">,<br /> array(</span><span style="color: #DD0000">'company.id'</span><span style="color: #007700">,</span><span style="color: #DD0000">'company.name'</span><span style="color: #007700">,</span><span style="color: #DD0000">'company.employee_of_the_month'</span><span style="color: #007700">,<br /> </span><span style="color: #DD0000">'department.id'</span><span style="color: #007700">,</span><span style="color: #DD0000">'department.name'</span><span style="color: #007700">,</span><span style="color: #DD0000">'employee.id'</span><span style="color: #007700">,</span><span style="color: #DD0000">'employee.name'</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">$acme </span><span style="color: #007700">= </span><span style="color: #0000BB">$root</span><span style="color: #007700">[</span><span style="color: #DD0000">'company'</span><span style="color: #007700">][</span><span style="color: #0000BB">0</span><span style="color: #007700">];<br /><br /></span><span style="color: #0000BB">$shoe </span><span style="color: #007700">= </span><span style="color: #0000BB">$acme</span><span style="color: #007700">-></span><span style="color: #0000BB">department</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">];<br /></span><span style="color: #0000BB">$sue </span><span style="color: #007700">= </span><span style="color: #0000BB">$shoe </span><span style="color: #007700">-> </span><span style="color: #0000BB">employee</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">];<br /><br /></span><span style="color: #0000BB">$it </span><span style="color: #007700">= </span><span style="color: #0000BB">$acme</span><span style="color: #007700">-></span><span style="color: #0000BB">createDataObject</span><span style="color: #007700">(</span><span style="color: #DD0000">'department'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$it</span><span style="color: #007700">-></span><span style="color: #0000BB">name </span><span style="color: #007700">= </span><span style="color: #DD0000">'IT'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$it</span><span style="color: #007700">-></span><span style="color: #0000BB">location </span><span style="color: #007700">= </span><span style="color: #DD0000">'G-block'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$billy </span><span style="color: #007700">= </span><span style="color: #0000BB">$it</span><span style="color: #007700">-></span><span style="color: #0000BB">createDataObject</span><span style="color: #007700">(</span><span style="color: #DD0000">'employee'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$billy</span><span style="color: #007700">-></span><span style="color: #0000BB">name </span><span style="color: #007700">= </span><span style="color: #DD0000">'Billy'</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$acme</span><span style="color: #007700">-></span><span style="color: #0000BB">name </span><span style="color: #007700">= </span><span style="color: #DD0000">'MegaCorp'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$shoe</span><span style="color: #007700">-></span><span style="color: #0000BB">name </span><span style="color: #007700">= </span><span style="color: #DD0000">'Footwear'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$sue</span><span style="color: #007700">-></span><span style="color: #0000BB">name </span><span style="color: #007700">= </span><span style="color: #DD0000">'Susan'</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$acme</span><span style="color: #007700">-></span><span style="color: #0000BB">employee_of_the_month </span><span style="color: #007700">= </span><span style="color: #0000BB">$billy</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$das </span><span style="color: #007700">-> </span><span style="color: #0000BB">applyChanges</span><span style="color: #007700">(</span><span style="color: #0000BB">$dbh</span><span style="color: #007700">, </span><span style="color: #0000BB">$root</span><span style="color: #007700">);<br />echo </span><span style="color: #DD0000">"Wrote back company with extra department and employee and all the names changed (Megacorp/Footwear/Susan)\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> <p class="para"> <div class="example" id="example-5476"> <p><strong>Example #3 One company, two departments, two employees - Retrieve and delete</strong></p> <div class="example-contents"><p> The company is retrieved as a complete data graph containing five data objects - the company, two departments and two employees. They are all deleted by deleting the company object. Deleting an object from the graph deletes all the object beneath it in the graph. Five SQL DELETE statements will be generated and executed. As always they will be qualified with a WHERE clause that contains all of the fields that were retrieved, so that any updates to the data in the database in the meantime by another process will be detected. </p></div> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #007700">require_once </span><span style="color: #DD0000">'SDO/DAS/Relational.php'</span><span style="color: #007700">;<br />require_once </span><span style="color: #DD0000">'company_metadata.inc.php'</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">/*************************************************************************************<br />* Now read it one more time and delete it.<br />* You can delete part, apply the changes, then carry on working with the same graph but<br />* care is needed to keep closure - you cannot delete the employee who is eotm without<br />* reassigning. For safety here we delete the company all in one go. <br />*************************************************************************************/<br /></span><span style="color: #0000BB">$das </span><span style="color: #007700">= new </span><span style="color: #0000BB">SDO_DAS_Relational </span><span style="color: #007700">(</span><span style="color: #0000BB">$database_metadata</span><span style="color: #007700">,</span><span style="color: #DD0000">'company'</span><span style="color: #007700">,</span><span style="color: #0000BB">$SDO_containment_metadata</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$dbh </span><span style="color: #007700">= new </span><span style="color: #0000BB">PDO</span><span style="color: #007700">(</span><span style="color: #0000BB">PDO_DSN</span><span style="color: #007700">,</span><span style="color: #0000BB">DATABASE_USER</span><span style="color: #007700">,</span><span style="color: #0000BB">DATABASE_PASSWORD</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$root </span><span style="color: #007700">= </span><span style="color: #0000BB">$das</span><span style="color: #007700">-></span><span style="color: #0000BB">executeQuery</span><span style="color: #007700">(</span><span style="color: #0000BB">$dbh</span><span style="color: #007700">,<br /> </span><span style="color: #DD0000">"select c.id, c.name, c.employee_of_the_month, d.id, d.name, e.id, e.name " </span><span style="color: #007700">.<br /> </span><span style="color: #DD0000">"from company c, department d, employee e " </span><span style="color: #007700">.<br /> </span><span style="color: #DD0000">"where e.dept_id = d.id and d.co_id = c.id and c.name='MegaCorp';"</span><span style="color: #007700">,<br /> array(</span><span style="color: #DD0000">'company.id'</span><span style="color: #007700">,</span><span style="color: #DD0000">'company.name'</span><span style="color: #007700">,</span><span style="color: #DD0000">'company.employee_of_the_month'</span><span style="color: #007700">,<br /> </span><span style="color: #DD0000">'department.id'</span><span style="color: #007700">,</span><span style="color: #DD0000">'department.name'</span><span style="color: #007700">,</span><span style="color: #DD0000">'employee.id'</span><span style="color: #007700">,</span><span style="color: #DD0000">'employee.name'</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">$megacorp </span><span style="color: #007700">= </span><span style="color: #0000BB">$root</span><span style="color: #007700">[</span><span style="color: #DD0000">'company'</span><span style="color: #007700">][</span><span style="color: #0000BB">0</span><span style="color: #007700">];<br /><br />unset(</span><span style="color: #0000BB">$root</span><span style="color: #007700">[</span><span style="color: #DD0000">'company'</span><span style="color: #007700">]);<br /></span><span style="color: #0000BB">$das </span><span style="color: #007700">-> </span><span style="color: #0000BB">applyChanges</span><span style="color: #007700">(</span><span style="color: #0000BB">$dbh</span><span style="color: #007700">, </span><span style="color: #0000BB">$root</span><span style="color: #007700">);<br /><br />echo </span><span style="color: #DD0000">"Deleted the company, departments and employees all in one go.\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="sdodasrel.examples.two-table.html">Two-table examples</a></div> <div class="next" style="text-align: right; float: right;"><a href="sdodasrel.limitations.html">Limitations</a></div> <div class="up"><a href="sdodasrel.examples.html">Examples</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>