<< Content classes  Content datatypes >> 

Content objects

This is an introduction on how to use the core classes of Exponential at a low level. With these classes you can create content object and read their contents ammong other things.

Fetching a Content Object

Before you can fetch a content object you need to know which id this content object has. Every content object in Exponential has it's own unique id. This id is used to identify the different objects.

When you know which id your content object has you can fetch the object directly with the eZContentObject::fetch() function. The example below shows how you can fetch a content object, the variable $contentObject is a eZContentObject object.

include_once( "kernel/classes/ezcontentobject.php" );

// fetch object with id 42
$contentObject = eZContentObject::fetch( 42 );

Reading the data

Object versions

Every content object consits of one or more versions. Normally you want to fetch the content from the current version. This is done with the function $contentObject->currentVersion(). In the code snippet below the variable $currentVersion stores a reference to the current document version of $contentObject. Versions of content objects are returned as instances of eZContentObjectVersion.

// Fetch the current version of the document
$currentVersion =& $contentObject->currentVersion();

Object translations

When you have the version object you can get the data. The data is returned with the $currentVersion->attributes() function. This function needs a parameter where you specify the language code, e.g. en_GB for the english translation of the object.

// fetch the english translation of the current version
$attributes =& $currentVersion->attributes( "en_GB" );

To get the available translations you use the function $currentVersion->translations(). This returns an array of eZContentObjectTranslation objects.

Reading the attributes

You can get the attributes from both eZContentObjectVersion and the eZContentObjectTranslation objects.

The $attributes variable is an array of eZContentObjectAttribute objects. Each attribute has each own contents. For example an article can have the following attributes:

  • Title - string
  • Intro - text
  • Body - text

The code snippet below loops all the attributes and prints the attribute name, datatype and contents. NOTE: Here we print the contents of the attribute directly. This can be a complex type, this is just a simplified example.

// Loop each attribute
foreach ( $attributes as $attribute )
{
    print( "Attribute name:" . $attribute->contentClassAttributeName() );
	// fetch the content attribute class object, to get the datatype
    $contentClassAttribute =& $attribute->contentClassAttribute();
    print( "Datatype: " . $contentClassAttribute->attribute( "data_type_string" ) );

	// print the content of the attribute
    print( "Content : " . $attribute->content() );
}

Storing changes in an object

Since all content which is stored in objects are located in the attribute you need to fetch the attributes for the version and translation you want to store changes in. When you have the attributes you can store the new content on the attributes with the function $attribute->store().


$attribute->setContent( "355" );

if ( $attribute->isValid() )
{
    $attribute->store();
}

Creating


Exponential