|
Setting and Getting Property ValuesThe following examples assume $company is the root of a tree of data objects created from the schema and instance document shown above.
Пример #1 Access via property name Data object properties can be accessed using the object property access syntax. The following sets the company name to 'Acme'.
<?php
Пример #2 Access via property name as array index We can also access properties using associative array syntax. The simplest form of this uses the property name as the array index. For example, the following sets the company name and gets the employeeOfTheMonth.
<?php
Пример #3 Data Object iteration We can iterate over the properties of a data object using foreach. The following iterates over the properties of the employee of the month.
<?php which will output: name: Jane Doe SN: E0003 The 'manager' property is not output, because it has not been set.
Пример #4 Access many-valued property by name Many-valued data object properties can also be accessed using the object property name syntax. The following gets the list of departments.
<?php
Пример #5 Many-valued element access We can access individual elements of many-valued properties using array syntax. The following accesses the first department in the company.
<?php
Пример #6 Many-valued property iteration Many-valued properties can also be iterated over using foreach. The following iterates over the company's departments.
<?php Each iteration will assign the next department in the list to the variable $department.
Пример #7 Chained property access We can chain property references on a single line. The following sets and gets the name of the first department.
<?php Using the associative array syntax, this is equivalent to
<?php In either case, the dept_name variable is set to 'Emerging Technologies'.
Пример #8 XPath navigation The associative array index can be an XPath-like expression. Valid expressions are defined by an augmented sub-set of XPath. 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:
<?php and the SDO XPath extension syntax is:
<?php Both these examples get the second employee from the first department.
Пример #9 XPath querying 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.
<?php
Пример #10 Creating child data objects 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.
<?php
Пример #11 Unset a primitive property The following clears the name of the first department.
<?php
Пример #12 Unset a data object unset can also be used to remove a data object from the tree. The following example shows John Jones leaving the company.
<?php
Пример #13 Unset a referenced data object 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.
<?php
Пример #14 Access via property index 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 Access via property name
<?php 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. |
|