<!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>One-table examples</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="sdodasrel.metadata.html">Specifying the metadata</a></div> <div class="next" style="text-align: right; float: right;"><a href="sdodasrel.examples.two-table.html">Two-table examples</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.one-table" class="section"> <h2 class="title">One-table examples</h2> <p class="para"> The following set of examples all use the Relational DAS to work with a data graph containing just one application data object, a single company and the data just to be found the company table. These examples do not exercise the power of SDO or the Relational DAS and of course the same result could be achieved more economically with direct SQL statements but they are intended to illustrate how to work with the Relational DAS. </p> <p class="para"> For this very simple scenario it would be possible to simplify the database metadata to include just the company table - if that were done the second and third arguments to the constructor and the column specifier used in the query example would become optional. </p> <p class="para"> <div class="example" id="example-5467"> <p><strong>Example #1 Creating a data object</strong></p> <div class="example-contents"><p> The simplest example is that of creating a single data object and writing it to the database. In this example a single company object is created, its name is set to 'Acme', and the Relational DAS is called to write the changes to the database. The company name is set here using the property name method. See the <a href="sdo.examples.html" class="link">Examples</a> section on the SDO extension for other ways of accessing the properties of an object. </p></div> <div class="example-contents"><p> Data objects can only be created when you have a data object to start with, however. It is for that reason that the first call to the Relational DAS here is to obtain a root object. This is in effect how to ask for an empty data graph - the special root object is the true root of the tree. The company data object is then created with a call to <span class="function"><strong>createDataObject()</strong></span> on the root object. This creates the company data object and inserts it in the graph by inserting into a multi-valued containment property on the root object called 'company'. </p></div> <div class="example-contents"><p> When the Relational DAS is called to apply the changes a simple insert statement 'INSERT INTO company (name) VALUES ("Acme");' will be constructed and executed. The auto-generated primary key will be set into the data object and the change summary will be reset, so that it would be possible to continue working with the same data object, modify it, and apply the newer changes a second time. </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 />* Construct the DAS with the metadata<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 /><br /></span><span style="color: #FF8000">/**************************************************************<br />* Obtain a root object and create a company object underneath.<br />* Make a simple change to the data object. <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 /><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 /><br /></span><span style="color: #FF8000">/**************************************************************<br />* Get a database connection and write the object to the database<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">$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 /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> <p class="para"> <div class="example" id="example-5468"> <p><strong>Example #2 Retrieving a data object</strong></p> <div class="example-contents"><p> In this example a single data object is retrieved from the database - or possibly more than one if there is more than one company called 'Acme'. For each company returned, the <var class="varname"><var class="varname">name</var></var> and <var class="varname"><var class="varname">id</var></var> properties are echoed. </p></div> <div class="example-contents"><p> In this example the third argument to <span class="function"><strong>executeQuery()</strong></span>, the column specifier is needed as there are other tables in the metadata with column names of <var class="varname"><var class="varname">name</var></var> and <var class="varname"><var class="varname">id</var></var>. If there were no possible ambiguity it could be omitted. </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 />* Construct the DAS with the metadata<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 /><br /></span><span style="color: #FF8000">/**************************************************************<br />* Get a database connection<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 /><br /></span><span style="color: #FF8000">/**************************************************************<br />* Issue a query to obtain a company object - possibly more if they exist<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 name, id from company where name="Acme"'</span><span style="color: #007700">,<br /> array(</span><span style="color: #DD0000">'company.name'</span><span style="color: #007700">, </span><span style="color: #DD0000">'company.id'</span><span style="color: #007700">) );<br /><br /></span><span style="color: #FF8000">/**************************************************************<br />* Echo name and id <br />***************************************************************/<br /></span><span style="color: #007700">foreach (</span><span style="color: #0000BB">$root</span><span style="color: #007700">[</span><span style="color: #DD0000">'company'</span><span style="color: #007700">] as </span><span style="color: #0000BB">$company</span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">"Company obtained from the database has name = " </span><span style="color: #007700">. <br /> </span><span style="color: #0000BB">$company</span><span style="color: #007700">[</span><span style="color: #DD0000">'name'</span><span style="color: #007700">] . </span><span style="color: #DD0000">" and id " </span><span style="color: #007700">. </span><span style="color: #0000BB">$company</span><span style="color: #007700">[</span><span style="color: #DD0000">'id'</span><span style="color: #007700">] . </span><span style="color: #DD0000">"\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-5469"> <p><strong>Example #3 Updating a data object</strong></p> <div class="example-contents"><p> This example combines the previous two, in the sense that in order to be updated the object must first be retrieved. The application code reverses the company name (so 'Acme' becomes 'emcA') and then the changes are written back to the database in the same way that they were when the object was created. Because the query searches for the name both ways round the program can be run repeatedly to find the company and reverse its name each time. </p></div> <div class="example-contents"><p> In this example the same instance of the Relational DAS is reused for the <span class="function"><strong>applyChanges()</strong></span>, as is the PDO database handle. This is quite alright; it also alright to allow the previous instances to be garbage collected and to obtain new instances. No state data regarding the graph is held the Relational DAS once it has returned a data graph to the application. All necessary data is either within the graph itself, or can be reconstructed from the metadata. </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 />* Construct the DAS with the metadata<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 /><br /></span><span style="color: #FF8000">/**************************************************************<br />* Get a database connection<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 /><br /></span><span style="color: #FF8000">/**************************************************************<br />* Issue a query to obtain a company object - possibly more if they exist<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 name, id from company where name="Acme" or name="emcA"'</span><span style="color: #007700">,<br /> array(</span><span style="color: #DD0000">'company.name'</span><span style="color: #007700">, </span><span style="color: #DD0000">'company.id'</span><span style="color: #007700">) );<br /><br /></span><span style="color: #FF8000">/**************************************************************<br />* Alter the name of just the first company<br />***************************************************************/<br /></span><span style="color: #0000BB">$company </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 />echo </span><span style="color: #DD0000">"obtained a company with name of " </span><span style="color: #007700">. </span><span style="color: #0000BB">$company</span><span style="color: #007700">-></span><span style="color: #0000BB">name </span><span style="color: #007700">. </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$company</span><span style="color: #007700">-></span><span style="color: #0000BB">name </span><span style="color: #007700">= </span><span style="color: #0000BB">strrev</span><span style="color: #007700">(</span><span style="color: #0000BB">$company</span><span style="color: #007700">-></span><span style="color: #0000BB">name</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/**************************************************************<br />* Write the change back<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 /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> <p class="para"> <div class="example" id="example-5470"> <p><strong>Example #4 Deleting a data object</strong></p> <div class="example-contents"><p> Any companies called 'Acme' or its reverse 'emcA' are retrieved. They are then all deleted from the graph with unset. </p></div> <div class="example-contents"><p> In this example they are all deleted in one go by unsetting the containing property (the property defining the containment relationship). It is also possible to delete them individually. </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 />* Construct the DAS with the metadata<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 /><br /></span><span style="color: #FF8000">/**************************************************************<br />* Get a database connection<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 /><br /></span><span style="color: #FF8000">/**************************************************************<br />* Issue a query to obtain a company object - possibly more if they exist<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 name, id from company where name="Acme" or name="emcA"'</span><span style="color: #007700">,<br /> array(</span><span style="color: #DD0000">'company.name'</span><span style="color: #007700">, </span><span style="color: #DD0000">'company.id'</span><span style="color: #007700">) );<br /><br /></span><span style="color: #FF8000">/**************************************************************<br />* Delete any companies found from the data graph<br />***************************************************************/<br /></span><span style="color: #007700">unset(</span><span style="color: #0000BB">$root</span><span style="color: #007700">[</span><span style="color: #DD0000">'company'</span><span style="color: #007700">]);<br /><br /></span><span style="color: #FF8000">/**************************************************************<br />* Write the change(s) back<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 /></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.metadata.html">Specifying the metadata</a></div> <div class="next" style="text-align: right; float: right;"><a href="sdodasrel.examples.two-table.html">Two-table examples</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>