Dashboard > PHP Community > ... > Learn More > SDO with PHP
Log In   View a printable version of the current page.
SDO with PHP
Added by Simon Laws, last edited by Caroline Maynard on Sep 10, 2007  (view change)
Labels: 
(None)


Information and resources relating to the implementation of Service Data Objects as a PHP PECL extension.

A Quick Tour of SDO PHP

There are four main parts of the SDO PHP implementation

  • Service Data Objects (refered to as SDOs) - the data that the PHP programmer manipulates in a PHP script
  • Data Access Service (refered to as DAS) - provides the means for reading and writing data from/to a data resource
  • Type Model - describes the types of SDOs and primitives that may validly appear in the SDO tree, this is created using an API or by reading in an XSD (in the case of the XML DAS)
  • Change Summary - a record of how the data has been changed during script execution, for example, if I have an SDO representation of a shopping cart and I change the quantity ordered for a particular item from 1 to 2 then the change summary will record that the original value was 1 and the new value is 2. This is used by the DAS to write the data back to the data resource.

Service Data Object

Service Data Objects (SDOs) enable PHP applications to work with data from different sources (like a database query, an XML file, and a spreadsheet) using a single interface.

Each different kind of data source requires a Data Access Service (DAS) to provide access to the data it holds. In your PHP application, you use a DAS to create an SDO instance that represents some of the data in the data source. You can then set and get values in the SDO instance using the standard SDO interface. Finally, you use a DAS to write the modified data back to a data source, typically the same one. For example, here is the SDO_DAS_Relational being used to access a Company/Department/Employee relational database.

The same pattern of operation and SDO_DataObject interfaces work with the SDO_DAS_XML which is used to access XML formatted data.

A service data object can contain three types of properties

  • Primitive values, for example, strings and integers
  • Contained SDOs, for example, a Company SDO could contain Department SDOs and in this way the SDO tree is built
  • Referenced SDOs, for example, an Employee SDO could reference another Employee SDO to identify a career mentor relationship

To access these three types of properties from a SDO you can use object syntax:

//get the value of the property called 'name'
$company_name = $company->name;

Or associative array syntax

//get the value of the property called 'name'
$company_name = $company['name'];

Each of these properties can be single-valued, as in the above access examples, or many-valued, that is, represented as an array:

//get the many-valued property called 'departments', get the first department SDO
//and then get the primitive property called 'name'
$first_department_name = $company->departments[0]->name;

You can also use XPath notation to navigate through the tree of data objects:

$first_department_second_employee = $company["departments[1]/employees[2]"]

Clearly you can chain accessors together as shown. Values can also be set in the same way

$company->departments[0]->name = "new department name";

As you may have guessed from these access examples SDO organizes data so that it can be accessed easily by
property name. If you want to organize the data in the order it has been added to the SDO then you
can use a sequence:

$company_seq = $company->getSequence();
$company_seq->insert('My Company Address', NULL, 'address');
$company_seq->insert('MyCompany', NULL, 'name');

I you want to take a look at what your SDO looks like you can use the reflection interface.

$reflection = new SDO_Model_ReflectionDataObject($company);
print($reflection);

There are many more interfaces offered by SDO. Please see the documentation, examples and samples.

Data Access Service (DAS)

The DAS provides the mechanism for reading from and writing to data resources. Currently DAS implementations
have been created for relational databased access and for XML file access. The main functions that a DAS provides are for adding types to the type model and for loading and saving SDOs:

Add types to the type model

// for SDO_DAS_XML
$xmldas->addTypes("company.xsd");

// for SDO_DAS_Relational
$das = new SDO_DAS_Relational ($database_metadata,...);

Load data into SDOs

// for SDO_DAS_XML
$document = $xmldas->loadFile("${dirname}/company.xml");

// for SDO_DAS_Relational
$root = $das->executeQuery(...);

Save any changes made to SDOs

// for SDO_DAS_XML
$das->saveFile($document, "myfile.xml");

// for SDO_DAS_Relational
$das->applyChanges(..., $root);

Type Model

Type Model

The type model holds a description of the types that can validly be created in the SDO hierarchy.
In the case of the SDO_DAS_XML the type model is created by reading in an XSD file:

$xmldas = SDO_DAS_XML::create();
$xmldas->addTypes("company.xsd");

The SDO_DAS_Relation requires that the developer specify the type model so that it matches that part of the
relational database schema that will be read or updated.

$company_table = array (
	'name' => 'company',
	'columns' => array('id', 'name',  'employee_of_the_month'),
	'PK' => 'id',
	'FK' => array (
		'from' => 'employee_of_the_month',
		'to' => 'employee',
		),
	);
$database_metadata = array($company_table);
$das = new SDO_DAS_Relational ($database_metadata,...);

There are interfaces for reading the type model and for retrieving the type for an SDO. Please see the documentation, examples and samples.

Change Summary

The change summary is automatically maintained as changes are made to the tree of related SDOs. Change summary contains the original value of any changed property and the value it has been changed to. This information is used when the SDO is written out.

The change summary information can be retrieved from the each SDO but this is not usually necessary.

Documentation

The user document for SDO PHP is included in the PHP Manual:

Frequently Asked Questions

SDO FAQ

Examples and Samples

SDO Examples
SDO Samples

Articles & Tutorials

SDO Tutorial

PHP and SDO White Paper
An introduction to Service Data Objects for PHP
Streamline working with XML in PHP using Service Data Objects
Using PHP's SDO and SCA extensions (Tyler Anderson on developerWorks, July 2007)

Wish List

SDO Wish List

Talks

Service Data Objects - php|tek, May 2006, Orlando

PHP and SDO White Paper (PHP Community)
SDO Examples (PHP Community)
SDO FAQ (PHP Community)
SDO Samples (PHP Community)
SDO Tutorial (PHP Community)
SDO Wish List (PHP Community)

Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.4.5 Build:#708 Apr 12, 2007) - Bug/feature request - Contact Administrators