Instantiate the Free Object

Assuming you already know how to instantiate a Joomla! Article or Joomla! Category object, you'll see that it works the same way here, but one more step is required. As the Free Object is not natively link to any table in the database, it needs to be defined "manually".

Additional note: we'll use "#__cck_store_free_logs" as an example here, and this table does not come with the core package.

$content = new JCckContentFree;
$content->setTable( '#__cck_store_free_logs' ); @ SEBLOD 3.17.0

You're ready to move forward !



Create a new Free Item

After instantiating the object and defining the table, you only need to call the create method, by passing:

  • the Content Type you wish to use: 'log', or any other content type based on the Free Object
  • an array of properties $data for your Content Item.
$content = new JCckContentFree;
$content->setTable( '#__cck_store_free_logs' );
$data      = array(
                      'state'=>1,
                      'title'=>'My Item 01'
                  );
$content->create( $content_type, $data ); @ SEBLOD 3.17.0

You've just created your 1st Free Content Item using JCckContent !

$content = new JCckContentFree;
$content->setTable( '#__cck_store_free_logs' );  
$data      = array(
                      'state'=>1,
                      'title'=>'My Item 01'
                  );

if ( $content->create( $content_type, $data )->isSuccessful() ) {
    // Do something
    echo $content->getPk();
} @ SEBLOD 3.17.0

Assuming you need to execute some custom code after creating an item, you should make sure everything went ok before doing it.
Calling the getPk method will return the newly created Primary Key (of the "base" table).

$content = new JCckContentFree;
$data      = array(
                      'color'=>'blue',
                      'snippet'=>'Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
                      'state'=>1,
                      'title'=>'My Item 01'
                  );

if ( $content->create( $content_type, $data )->isSuccessful() ) {
    // Do something
} @ SEBLOD 3.17.0

After creating your custom fields, you just need to complete the $data array with the column names of these fields.

Additional note: the create method will keep a static version of the object, in case it needs to be reused later on.



Load an existing Free Item

Various calls of the static method getInstance give you the ability to load an existing Free Item. Argument depends of the class used.

$content = JCckContentFree::getInstance( $pk );

OR

$content = JCckContent::getInstance( array( 'free', $pk ) );

OR

$content = JCckContent::getInstance( $id ); @ SEBLOD 3.17.0

All the above is identical, but the 1st one is the most straightforward. Using the subclass, simply pass the Primary Key of your free item, as the Object is already known (in opposition of the 3rd one which takes the SEBLOD IDentifier).


Additional note: the getInstance method will keep a static version of the object, in case it needs to be reused later on.
It means that if you call again a getInstance with a known identifier, it will return the object directly without reloading everything.

$content = new JCckContentFree;
$content->load( $pk ); @ SEBLOD 3.17.0

An alternative way of loading a Content Item is to use the load method, after instantiating the object.
This can be useful to load items within a loop, or when you need to call more than one "starting" method from a single place.

Additional note: the load method doesn't keep a static version of the object, neither uses a static version while loading an item.

if ( !$content->isSuccessful() ) {
    return;
}
// Do something

OR

if ( $content->isSuccessful() ) {
    // Do something
}

After loading an item, you may need to execute some custom code. It may not always be needed (depending on what you'll do next), but we recommend to make sure everything went ok before continuing (cf above).
So what should you do next ?

// Do something
echo $content->getProperty( 'title' );

You could retrieve the title of your Free Item.

// Do something
echo $content->getProperty( 'snippet' );

... or any other custom property (from any other table instance).

Additional resources:  List of all "get" and "set" methods



Delete an existing Free Item

One call of the  delete method will remove everything related to the Free Item.

$content->delete();

Assuming you have already loaded an Content Item in the previous section, that is all you need !

$content = new JCckContentFree;
$content->delete( $pk ); @ SEBLOD 3.17.0

If you start from nothing, after instantiating the object, make sure to pass the Primary Key of the Free Item you wish to delete.



Update an existing Joomla! Item

After loading an existing item, updating it will consist most of the time in calling both the  setProperty method (once or more than once), and then the store method. As JCckContent class supports method chaining, you can chain all calls.

$content->setProperty( 'title', 'My Title' )
             ->setProperty( 'snippet', 'My Snippet' )
             ->store();

This will set (or change) the value of two properties within the current instance, and then update the DB.

Additional note: the store method will actually call the store function of each table instance (where properties have been changed).

if ( $content->setProperty( 'title', 'My Title' )->store() ) {
    // Ok

For now, all methods performing an update (in DB) of an existing content item DO NOT support method chaining. So you can't use the isSuccessful method to test if everything went ok, you will need to test the return of the store method (boolean: true/false).