📄 sdo.sample.getset.html
字号:
<div class="example"> <p><b>Example #8 XPath navigation</b></p> <div class="example-contents"><p> The associative array index can be an XPath-like expression. Valid expressions are defined by an augmented sub-set of XPath. </p></div> <div class="example-contents"><p> Two forms of indexing into many-valued properties are supported. The first is the standard XPath array syntax with the indexing starting at one, the second is an SDO extension to XPath with an index starting at zero. The standard syntax is: </p></div> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br /> $jane_doe </span><span style="color: #007700">= </span><span style="color: #0000BB">$company</span><span style="color: #007700">[</span><span style="color: #DD0000">"departments[1]/employees[2]"</span><span style="color: #007700">];<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> <div class="example-contents"><p>and the SDO XPath extension syntax is:</p></div> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br /> $jane_doe </span><span style="color: #007700">= </span><span style="color: #0000BB">$company</span><span style="color: #007700">[</span><span style="color: #DD0000">"departments.0/employees.1"</span><span style="color: #007700">];<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> <div class="example-contents"><p> Both these examples get the second employee from the first department. </p></div> </div> </p> <p class="para"> <div class="example"> <p><b>Example #9 XPath querying</b></p> <div class="example-contents"><p> We can use XPath to query and identify parts of a data object based on instance data. The following retrieves the manager from the 'Advanced Technologies' department. </p></div> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br /> $ad_tech_mgr </span><span style="color: #007700">= <br /> </span><span style="color: #0000BB">$company</span><span style="color: #007700">[</span><span style="color: #DD0000">"departments[name='Advanced Technologies']/employees[manager=true]"</span><span style="color: #007700">];<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> </p> <p class="para"> <div class="example"> <p><b>Example #10 Creating child data objects</b></p> <div class="example-contents"><p> A data object can be a factory for its child data objects. A child data object is automatically part of the data graph. The following add a new employee to the 'Advanced Technologies' department. </p></div> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br /> $ad_tech_dept </span><span style="color: #007700">= </span><span style="color: #0000BB">$company</span><span style="color: #007700">[</span><span style="color: #DD0000">"departments[name='Advanced Technologies']"</span><span style="color: #007700">];<br /> </span><span style="color: #0000BB">$new_hire </span><span style="color: #007700">= </span><span style="color: #0000BB">$ad_tech_dept</span><span style="color: #007700">-></span><span style="color: #0000BB">createDataObject</span><span style="color: #007700">(</span><span style="color: #DD0000">'employees'</span><span style="color: #007700">);<br /> </span><span style="color: #0000BB">$new_hire</span><span style="color: #007700">-></span><span style="color: #0000BB">name </span><span style="color: #007700">= </span><span style="color: #DD0000">'John Johnson'</span><span style="color: #007700">;<br /> </span><span style="color: #0000BB">$new_hire</span><span style="color: #007700">-></span><span style="color: #0000BB">SN </span><span style="color: #007700">= </span><span style="color: #DD0000">'E0005'</span><span style="color: #007700">;<br /> </span><span style="color: #0000BB">$new_hire</span><span style="color: #007700">-></span><span style="color: #0000BB">manager </span><span style="color: #007700">= </span><span style="color: #0000BB">false</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> </p> <p class="para"> <div class="example"> <p><b>Example #11 Unset a primitive property</b></p> <div class="example-contents"><p> We can use the <a href="function.isset.html" class="function">isset()</a> and <a href="function.unset.html" class="function">unset()</a> functions to test and remove items from the data object. </p></div> <div class="example-contents"><p> The following clears the name of the first department. </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">unset(</span><span style="color: #0000BB">$company</span><span style="color: #007700">-></span><span style="color: #0000BB">departments</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">]-></span><span style="color: #0000BB">name</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> </p> <p class="para"> <div class="example"> <p><b>Example #12 Unset a data object</b></p> <div class="example-contents"><p> unset can also be used to remove a data object from the tree. The following example shows John Jones leaving the company. </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">unset(</span><span style="color: #0000BB">$company</span><span style="color: #007700">-></span><span style="color: #0000BB">departments</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">]-></span><span style="color: #0000BB">employees</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">]);<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> </p> <p class="para"> <div class="example"> <p><b>Example #13 Unset a referenced data object</b></p> <div class="example-contents"><p> The following removes the 'employeeOfTheMonth' from the company. If this were a containment relationship then the employee would be removed from the company (probably not a good idea to sack your best employee each month!), but since this is a non-containment reference, the employee being referenced will remain in the department in the company, but will no longer be accessible via the employeeOfTheMonth property. </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">if (isset(</span><span style="color: #0000BB">$company</span><span style="color: #007700">-></span><span style="color: #0000BB">employeeOfTheMonth</span><span style="color: #007700">)) {<br /> unset(</span><span style="color: #0000BB">$company</span><span style="color: #007700">-></span><span style="color: #0000BB">employeeOfTheMonth</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"> <p><b>Example #14 Access via property index</b></p> <div class="example-contents"><p> Data object properties can be accessed via their property index using array syntax. The property index is the position at which the property's definition appears in the model (in this case the xml schema). We can see from the schema listing above that the company name attribute is the second company property (the SDO interface makes no distinction between XML attributes and elements). The following sets the company name to 'Acme', with the same result as <a href="sdo.sample.getset.html#sdo.examples.propname" class="link">Access via property name</a> </p></div> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br /> $company</span><span style="color: #007700">[</span><span style="color: #0000BB">1</span><span style="color: #007700">] = </span><span style="color: #DD0000">'Acme'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> <div class="example-contents"><p> Using the index directly in this way is likely to be fragile. Normally the property name syntax should be preferred, but the property index may be required in special cases. </p></div> </div> </p> </div><hr /><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="sdo.examples.html">Examples</a></div> <div class="next" style="text-align: right; float: right;"><a href="sdo.sample.sequence.html">Working with Sequenced Data Objects</a></div> <div class="up"><a href="sdo.examples.html">Examples</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -